question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

document.add_picture ZeroDivisionError: division by zero

See original GitHub issue

Hello,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:open
  • Created 5 years ago
  • Reactions:1
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
Swannbmcommented, Nov 1, 2020

I think it’s a True issue because in readthedoc it says that it should default to 72 DPI.

1reaction
floriaanpostcommented, Jul 1, 2020

In case anyone gets this error… It is “solved” by monkey patching the height and width of the Image class:

from docx.image.image import Image
from docx.shared import Inches

@property
def image_width(self):
    if (self.horz_dpi == 0):
        return Inches(self.px_width / 72)
    return Inches(self.px_width / self.horz_dpi)


@property
def image_height(self):
    if (self.vert_dpi == 0):
        return Inches(self.px_height / 72)
    return Inches(self.px_height / self.vert_dpi)


Image.width = image_width
Image.height = image_height

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found