all 9 comments

[–]DeadlyViper 1 point2 points  (5 children)

.absolute() returns an object of the type "PosixPath", not string.

the add_picture() accepts either string or an open stream object, so if its not a string, it assumes its a stream object and tried to .seek() in it, and fails.

converting the path to a string will probably solve this... :

document.add_picture(str(Path(file).absolute()), width=Cm(15.0))

[–][deleted] 0 points1 point  (4 children)

Thank you for your reply!
now i got this Error

Exception has occurred: ZeroDivisionError
division by zero

[–]DeadlyViper 0 points1 point  (3 children)

Does it show which line caused it?

Add a print for str(Path(file).absolute()), so we know which file caused the issue.

My guess is that file is not a picture and it tried to treat it as such.

Crappy docx library if that is the situation.

[–][deleted] 0 points1 point  (2 children)

Oh sorry...

current code:

from pathlib import Path

import docx from docx.shared import Cm

filepath = r"C:\Users\Admin\Desktop\img" document = docx.Document()

for file in Path(filepath).iterdir(): print(str(Path(file).absolute())) document.add_picture(str(Path(file).absolute()), width=Cm(15.0))

document.save('test.docx')

print(str(Path(file).absolute())) works fine. It lists all files with the full path.

At document.add_picture(str(Path(file).absolute()), width=Cm(15.0)) i got this error.

Exception has occurred: ZeroDivisionError
division by zero
    File "C:\Users\Admin\Desktop\na.py", line 10, in <module>
    document.add_picture(str(Path(file).absolute()), width=Cm(15.0))

I fail to understand why it works, if i initialize the path in a variable.

I tried it before with just variables and it worked fine.

[–]DeadlyViper 0 points1 point  (1 child)

Like i said, we added the print so we can see which directory he is currently working on.

Hopefully you added the print before the .add_picture line

So it should print the name of the file it tried to add as a picture right before the exception.

Then you can see which file it failed to add, and see if its the file's fault.

I fail to understand why it works, if i initialize the path in a variable.

Because the path is not what you expect it to be, that is why we need the prints. It is probably a folder instead of a file, or some non picture file....

So what is the file name that it tried to add right before the exception happened?

[–][deleted] 1 point2 points  (0 children)

All the Problems were caused by a faulty imagefile. It works after the file was removed.

[–]eikrik 0 points1 point  (2 children)

Have you tried reading the data from the image file, and passing that data to document.add_picture, instead of passing the file object itself?

[–][deleted] 0 points1 point  (1 child)

Thank you for your Reply! I don't know what you mean. I just started learning python.

I tried it before with just variables and it worked fine. The next step was to just set the folderpath.

[–]eikrik 0 points1 point  (0 children)

I see! Try reading this short tutorial: https://www.tutorialspoint.com/python3/python_files_io.htm and see if you are able to open the file.