YesIAmThatGuy
YesIAmThatGuy

Reputation: 97

Using python-docx to batch print the name of the last user to modify

I'm trying to use Python to print the names of the last users to modify a bunch of documents (docx). In reality, this script should return a bunch of different names. I'm having to mix a bunch of different modules, but either I'm doing something wrong, or docx doesn't like the batch work. Has anybody got more experience with this that might be able to find the issue?

from docx import Document
import docx, glob, os

for filename in glob.glob('/Users/owner/Desktop/Rename/*.docx'):
fullfilename=os.path.abspath(filename)

document = Document('fullfilename')
core_properties = document.core_properties
print(core_properties.last_modified_by)

For reference, I've merged two scripts for this, and docx seemed to work well when it was one file at a time, is there something going wrong with my loop?:

from docx import Document
import docx

document = Document('mine.docx')
core_properties = document.core_properties
print(core_properties.last_modified_by)

I'm using Python 3.4 and docx 0.8.6

Upvotes: 0

Views: 388

Answers (1)

scanny
scanny

Reputation: 29021

The main problem seems to be a lack of indenting. Python uses indentation (instead of curly braces) to determine what the block of your loop should be. So in this case it's only running through once. What you need is this:

import glob, os
from docx import Document

for filename in glob.glob('/Users/owner/Desktop/Rename/*.docx'):
    fullfilename = os.path.abspath(filename)
    document = Document('fullfilename')
    core_properties = document.core_properties
    print(core_properties.last_modified_by)

Note there is no need to import docx, since you don't use it (directly). In general, the Document object is all that needs importing in python-docx projects.

Upvotes: 0

Related Questions