Using python-docx to batch print the name of the last user to modify
<p>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?</p>

<pre><code>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)
</code></pre>

<p>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?:</p>

<pre><code>from docx import Document
import docx

document = Document('mine.docx')
core_properties = document.core_properties
print(core_properties.last_modified_by)
</code></pre>

<p>I'm using Python 3.4 and docx 0.8.6</p>

<p>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:</p>

<pre><code>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)
</code></pre>

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

python ms-office batch-processing python-docx