“最后修改者"是指使用Python的xlsx的(用户名,不是时间)属性 [英] "Last modified by" (user name, not time) attribute for xlsx using Python
问题描述
我需要能够使用Python查看xlsx文件的最后修改者"属性.我已经能够处理docx文件,并希望该体系结构足够相似,可以在其他Office应用程序上使用,但不幸的是没有.有人知道xlsx的类似模块吗?
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?
这是使用python-docx查看字段的脚本:
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)
我在这里使用Python 3.4和docx 0.8.6.
I'm using Python 3.4 and docx 0.8.6 here.
推荐答案
对于 .xlsx
文件,您可以使用此文件(将 filename
设置为您的 .xlsx
文件):
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)
我还没有测试过,但是我希望相同的代码也可以用于其他OOXML文件(例如 .docx
)
I haven't tested it, but I'd expect the same code to work for other OOXML files too (e.g. .docx
)
这篇关于“最后修改者"是指使用Python的xlsx的(用户名,不是时间)属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!