document.add_picture ZeroDivisionError: division by zero
See original GitHub issueHello,I have a question,i want to add a picture to doc Mycode:
from docx import Document
from docx.shared import Inches
document = Document()
document.add_picture('./test.jpg',width=Inches(1.25))
document.save('demo.docx')
Error:
`C:\Users\luowq\AppData\Local\Programs\Python\Python36\python3.exe F:/学习代码/study/python/imageCompareSvr/main.py
Traceback (most recent call last):
File "F:/学习代码/study/python/imageCompareSvr/main.py", line 28, in <module>
document.add_picture('./test.jpg',width=Inches(1.25))
File "C:\Users\luowq\AppData\Local\Programs\Python\Python36\lib\site-packages\docx\document.py", line 79, in add_picture
return run.add_picture(image_path_or_stream, width, height)
File "C:\Users\luowq\AppData\Local\Programs\Python\Python36\lib\site-packages\docx\text\run.py", line 62, in add_picture
inline = self.part.new_pic_inline(image_path_or_stream, width, height)
File "C:\Users\luowq\AppData\Local\Programs\Python\Python36\lib\site-packages\docx\parts\document.py", line 93, in new_pic_inline
cx, cy = image.scaled_dimensions(width, height)
File "C:\Users\luowq\AppData\Local\Programs\Python\Python36\lib\site-packages\docx\image\image.py", line 159, in scaled_dimensions
scaling_factor = float(width) / float(self.width)
File "C:\Users\luowq\AppData\Local\Programs\Python\Python36\lib\site-packages\docx\image\image.py", line 127, in width
return Inches(self.px_width / self.horz_dpi)
ZeroDivisionError: division by zero
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:5 (1 by maintainers)
Top Results From Across the Web
ZeroDivisionError division by zero in Python error handling
In this tutorial, we'll reproduce the issue and then go over some solutions. Reproducing the error#. Let's write the following program, divide.
Read more >Error python : [ZeroDivisionError: division by zero]
ZeroDivisionError : division by zero a = 10 b = 0 c = a/b. This would cause the type error because you are...
Read more >【Python】抓取网页图片并保存到本地和Word(ImagePIL和 ...
Return a new picture shape added in its own paragraph at the end of the document. The picture contains the image at *image_path_or_stream*, ......
Read more >Full text of "Python Ebooks" - Internet Archive
... in main resuit = number / othernumber ZeroDivisionError: division by zero ... Add the image: >>> image = document.addpicture('photo-dublin-al.jpg') 5.
Read more >Python error ZeroDivisionError division by zero - Edureka
I'm getting the following error while executing a python code: ZeroDivisionError: division by zero ... z = x/y ZeroDivisionError: division ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I think it’s a True issue because in readthedoc it says that it should default to 72 DPI.
In case anyone gets this error… It is “solved” by monkey patching the height and width of the Image class:
If don’t have a clue to why it is zero in the first place, as the images (in my case) do not appear to have an header, so it should default to 72 dpi.