1

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.

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)

Is there a similar way to do this with .xlsx files or am I out of luck?

Chandella07
  • 2,089
  • 14
  • 22
STRAT0CAST3R
  • 21
  • 1
  • 7

1 Answers1

7

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")
Chandella07
  • 2,089
  • 14
  • 22
  • Can I get the Excel cell's properties such as: border, background type? The xlrd package can do [such thing](https://stackoverflow.com/questions/12540856/how-to-get-excel-cell-properties-in-python) – Steven Lee Apr 11 '21 at 10:39