如何使用Python编辑.xlsx文件的核心属性? [英] How to edit core properties of .xlsx file with Python?
本文介绍了如何使用Python编辑.xlsx文件的核心属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试填充Excel文件的某些核心属性字段(即主题,类别和标题字段),但找不到解决方法.
I'm attempting to populate some of the core property fields for Excel files (namely the subject, category, and title fields) and am having trouble finding a way to do so.
我能够使用docx模块使用.docx文件完成此操作,
I was able to accomplish this with .docx files using the docx module like so:
doc = docx.Document(file)
name = doc.tables[1]._cells[43].text
data = doc.tables[0]._cells[1].text
numbers = re.findall('\d{9}', data)
doc.core_properties.title = numbers[0]
doc.core_properties.category = numbers[1]
doc.core_properties.subject = name
doc.save(file)
是否有类似的方法可以对.xlsx文件执行此操作,或者我不走运吗?
Is there a similar way to do this with .xlsx files or am I out of luck?
推荐答案
尝试使用 openpyxl
模块以交互方式获取和设置属性.
Try openpyxl
module to get and set properties interatively.
import openpyxl
fh = openpyxl.load_workbook("results.xlsx")
obj = fh.properties #To get old properties
print obj # print old properties
fh.properties.title = "newTitle" # To set title
fh.properties.category = "newCategory" # To set category
fh.properties.subject = "newSubject" # To set subject
##similarly you can set other fields ##
new_obj = fh.properties #Now get new properties
print new_obj # print new properties
fh.save("results.xlsx")
这篇关于如何使用Python编辑.xlsx文件的核心属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文