我尝试使用openpyxl将数据写入现有的xlsx工作簿并将其另存为单独的文件。我想在一张名为“数据”的表格中写一个数据帧。并将一些值写入另一张名为'摘要'然后将工作簿对象保存为' test.xlsx'。我有以下代码将我需要的计算写入摘要表,但是我得到了一个意外关键字参数的TypeError' font'当我尝试写日期框架时,对我来说没有多大意义......
我使用以下代码,我已经改编自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()
追溯:
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'
答案 0 :(得分:1)
我认为错误源于openpyxl样式API的必要更改。因此,Pandas会安装旧版本的openpyxl。你应该没问题,如果你删除openpyxl 2.2