-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
document.add_picture ZeroDivisionError: division by zero #515
Comments
I've shortened your code to get rid of lines that I don't believe are related. Please ensure the error still occurs with this minimal code to reproduce. I believe you're going to find that your JPEG file is missing some important information, in particular, the horizontal DPI setting. Try using another JPEG file and seeing if the error reoccurs. |
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. |
I think it's a True issue because in readthedoc it says that it should default to 72 DPI. |
Thanks! I had the same issue. Jpegs with bad header. Hope this gets fixed in the main branch. |
So I couldn't get this monkey patch to work, so I set out to try and fix the actual bug. A bit beyond my pay grade, but I did track it down to this function in tiff.py: ` def _dpi(self, resolution_tag):
That error trap simply isn't going off.. because the photos do have resolution tags. There's something else that's preventing these values from getting passed correctly, but that's why it's not defaulting. Anyways, I did manage to get it to work simply put putting line 343 of tiff.py in a try/except block and setting a default value of 72 there. I don't even know if this is referring to resolution... but I figured it had something to do with sizing so I started there, and it worked. Hope that gives a starting point to anyone
|
Had the same issue in 2024, the bug still exists. |
Just in case, I solved this using Pillow. It's probably a bit overkill but hey, it works. # before:
picture_stream = BytesIO(picture_content)
run = paragraph.add_run()
run.add_picture(picture_stream) # raises the ZeroDivisionError
# after:
picture_stream = BytesIO(picture_content)
image = Image.open(picture_stream)
clean_stream = BytesIO()
image.save(clean_stream, format=image.format)
clean_stream.seek(0)
run = paragraph.add_run()
run.add_picture(clean_stream) |
Hello,I have a question,i want to add a picture to doc
Mycode:
The text was updated successfully, but these errors were encountered: