“Last modified by” (user name, not time) attribute for xlsx using Python

99封情书 提交于 2020-01-24 21:50:51

问题


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.


回答1:


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)




回答2:


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']



回答3:


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



来源:https://stackoverflow.com/questions/39944495/last-modified-by-user-name-not-time-attribute-for-xlsx-using-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!