Skip to content

document.add_picture ZeroDivisionError: division by zero #515

Open
@luowqn

Description

@luowqn

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions