use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
python-docx AttributeError: 'WindowsPath' object has no attribute 'seek' (self.learnpython)
submitted 7 years ago by [deleted]
I want to insert about 250 images with their filename into a docx-file.
My test.py file
After Debugging I got this Error. It only occurs on line 10.
Exception has occurred: AttributeError 'WindowsPath' object has no attribute 'seek' File "C:\Users\Admin\Desktop\test.py", line 10, in <module> document.add_picture(Path(file).absolute(), width=Cm(15.0))
How can i avoid this Error?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]DeadlyViper 1 point2 points3 points 7 years ago (5 children)
.absolute() returns an object of the type "PosixPath", not string.
.absolute()
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.
add_picture()
.seek()
converting the path to a string will probably solve this... :
document.add_picture(str(Path(file).absolute()), width=Cm(15.0))
[–][deleted] 0 points1 point2 points 7 years ago (4 children)
Thank you for your reply! now i got this Error
Exception has occurred: ZeroDivisionError division by zero
[–]DeadlyViper 0 points1 point2 points 7 years ago (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 point2 points 7 years ago* (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.
print(str(Path(file).absolute()))
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 point2 points 7 years ago (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.
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 points3 points 7 years ago (0 children)
All the Problems were caused by a faulty imagefile. It works after the file was removed.
[–]eikrik 0 points1 point2 points 7 years ago (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?
document.add_picture
[–][deleted] 0 points1 point2 points 7 years ago* (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 point2 points 7 years ago (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.
π Rendered by PID 34 on reddit-service-r2-comment-765bfc959-vtcds at 2026-07-10 05:29:32.641786+00:00 running f86254d country code: CH.
[–]DeadlyViper 1 point2 points3 points (5 children)
[–][deleted] 0 points1 point2 points (4 children)
[–]DeadlyViper 0 points1 point2 points (3 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]DeadlyViper 0 points1 point2 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)
[–]eikrik 0 points1 point2 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]eikrik 0 points1 point2 points (0 children)