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.

YesIAmThatGuy
  • 87
  • 4
  • 12

3 Answers3

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)

cco
  • 5,873
  • 1
  • 16
  • 21
0

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']
philologon
  • 2,093
  • 4
  • 19
  • 35
-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

rafalf
  • 425
  • 7
  • 16
  • 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. – YesIAmThatGuy Oct 09 '16 at 14:25
  • working in python 3 as far as i am aware. you are looking for a *by*. sorry misread your message – rafalf Oct 09 '16 at 14:33
  • 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. :) – YesIAmThatGuy Oct 09 '16 at 14:35
  • 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 – YesIAmThatGuy Oct 09 '16 at 14:42