如何使用 Python 编辑 .xlsx 文件的核心属性?

STR*_*T3R 1 python excel fileinfo python-3.x

我正在尝试填充 Excel 文件的一些核心属性字段(即主题、类别和标题字段),但找不到这样做的方法。

我能够使用 docx 模块使用 .docx 文件完成此操作,如下所示:

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)
Run Code Online (Sandbox Code Playgroud)

是否有类似的方法可以使用 .xlsx 文件执行此操作,还是我不走运?

Cha*_*a07 5

尝试openpyxl模块以交互方式获取和设置属性。

    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")
Run Code Online (Sandbox Code Playgroud)