2

I need to be able to view the "last modified by" attribute for xlsx files using Python. I've been able to do for docx files, and was hoping that the architecture would be similar enough to use on other Office applications, but unfortunately not. Does anybody know of a similar module for xlsx?

This is the script to view the field using python-docx:

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 here.

4
  • Just to reiterate: by "last modified by" I'm looking for a username, not a time. :) Commented Oct 9, 2016 at 14:26
  • The method provided in stackoverflow.com/a/7021492/293494 should probably work for .xlsx files. Commented Oct 9, 2016 at 14:38
  • Thanks mkj, that looks interesting. I'll take a look and let you know how I get on. Commented Oct 9, 2016 at 14:45
  • No joy unfortunately :( xlrd has a field for "owner" but not for the last user to update the file Commented Oct 9, 2016 at 15:15

3 Answers 3

3

For .xlsx files, you can use this (set filename to the name of your .xlsx file):

import xml.etree.ElementTree
import xml.etree.cElementTree as ET
import zipfile

corePropNS = '{http://schemas.openxmlformats.org/package/2006/metadata/core-properties}'

zf = zipfile.ZipFile(filename, 'r')
part = zf.open('docProps/core.xml', 'r')
tree = ET.XML(part.read())
lastModifiedBy = tree.find(corePropNS+'lastModifiedBy').text

print(lastModifiedBy)

I haven't tested it, but I'd expect the same code to work for other OOXML files too (e.g. .docx)

Sign up to request clarification or add additional context in comments.

Comments

-1
import os
filename = "C:\\test.xlsx"
statsbuf = os.stat(filename)
print "modified:",statsbuf.st_mtime 



f = os.path.getmtime('C:\\test.xlsx')
print f

since the beginning

4 Comments

Thanks for the post. That's set for finding the modification time, rather than the user that last made a change. It's also set for Python 2 rather than 3.
working in python 3 as far as i am aware. you are looking for a by. sorry misread your message
That's ok, the title of my question probably wasn't as clear as it could have been, so I've changed it now. For 3 you need brackets around the print function. :)
It really isn't in the grand scheme of things. Eventually this will be built into a function that renames the file based on the last person to modify it (it's for timesheets at work). This snippet is driving me mad tho, it's turned out to be the be the trickiest part! If you have any other ideas, please let me know
-1

Sorry I'm late, but here is what I got to work.

    import xlrd
    wb = xlrd.open_workbook(a_file)
    worksheet =  wb.sheet_by_index(0)
    mod_by = worksheet.book.props['last_modified_by']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.