1

I'm trying to use openpyxl to write data to an existing xlsx workbook and save it as a separate file. I would like to write a dataframe to a sheet called 'Data' and write some values to another sheet called 'Summary' then save the workbook object as 'test.xlsx'. I have the following code that writes the calculations I need to the summary sheet, but I get a TypeError for an unexpected keyword argument 'font' when I try to write the dateframe, which doesn't make much sense to me...

I'm using the following code which I've adapted from here

from openpyxl import load_workbook
import pandas as pd

book = load_workbook('template.xlsx')

# Write values to summary sheet
ws = book.get_sheet_by_name('Summary')
ws['A1'] = 'TEST'

book.save('test.xlsx')

# Write df 
writer = pd.ExcelWriter('test.xlsx', engine='openpyxl') 
writer.book = book
writer.sheets = dict((ws.title, ws) for ws in book.worksheets)
df.to_excel(writer, sheet_name="Data", index=False)
writer.save()

The trace-back:

File "C:\Users\me\AppData\Local\Continuum\Anaconda\lib\site-    packages\pandas\core\frame.py", line 1274, in to_excel
  startrow=startrow, startcol=startcol)

File "C:\Users\me\AppData\Local\Continuum\Anaconda\lib\site-packages\pandas\io\excel.py", line 778, in write_cells
  xcell.style = xcell.style.copy(**style_kwargs)

File "C:\Users\me\AppData\Local\Continuum\Anaconda\lib\site-packages\openpyxl-2.2.2-py2.7.egg\openpyxl\compat\__init__.py", line 67, in new_func
  return obj(*args, **kwargs)

TypeError: copy() got an unexpected keyword argument 'font'
Community
  • 1
  • 1
rwester
  • 154
  • 9

1 Answers1

1

I think the error arises from necessary changes in the openpyxl styles API. As a result Pandas installs an older version of openpyxl. You should be okay, therefore, if you remove openpyxl 2.2

Charlie Clark
  • 18,477
  • 4
  • 49
  • 55