【问题标题】:How to edit core properties of .xlsx file with Python?如何使用 Python 编辑 .xlsx 文件的核心属性?
【发布时间】:2018-08-31 17:19:25
【问题描述】:

我正在尝试填充 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)

有没有类似的方法来处理 .xlsx 文件,还是我不走运?

【问题讨论】:

    标签: python excel python-3.x fileinfo


    【解决方案1】:

    尝试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")
    

    【讨论】:

    • 能否获取 Excel 单元格的属性,例如:边框、背景类型? xlrd 包可以做such thing
    猜你喜欢
    • 2011-06-30
    • 1970-01-01
    • 1970-01-01
    • 2019-11-02
    • 2018-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-30
    相关资源
    最近更新 更多