Modifying an existing excel workbook's multiple worksheets based on pandas dataframe

南楼画角 提交于 2019-12-08 13:24:49

问题


I currently have an excel file with, for minimally viable example, say 3 sheets. I want to change 2 of those sheets to be based on new values coming from 2 pandas dataframes (1 dataframe for each sheet).

This is the code I currently have:

from openpyxl.writer.excel import ExcelWriter
from openpyxl import load_workbook

path = r"Libraries\Documents\Current_Standings.xlsx"
book = load_workbook('Current_Standings.xlsx')
writer = pd.ExcelWriter(path, 'Current_Standings.xlsx', 
engine='openpyxl')
writer.book = writer
Blank_Propensity_Scores.to_excel(writer, sheet_name = 
'Blank_Propensity.xlsx')
Leads_by_Rep.to_excel(writer,sheet_name = 'Leads_by_Rep.xlsx')
writer.save()

when I run this I get the following error message, not sure why, because every stack overflow answer I have looked at has only 1 item for openpyxl:

TypeError: __new__() got multiple values for argument 'engine'

I also tried playing around with getting rid of the engine='openpyxl' argument but when I do that I get the following error message instead:

ValueError: No Excel writer 'Current_Standings.xlsx'

回答1:


If you execute on your Python command line the command 'help(pd.ExcelWriter)' you will see the parameters on the first lines:

class ExcelWriter(builtins.object)
 |  Class for writing DataFrame objects into excel sheets, default is to use
 |  xlwt for xls, openpyxl for xlsx.  See DataFrame.to_excel for typical usage.
 |
 |  Parameters
 |  ----------
 |  path : string
 |      Path to xls or xlsx file.
 |  engine : string (optional)
 |      Engine to use for writing. If None, defaults to
 |      ``io.excel.<extension>.writer``.  NOTE: can only be passed as a keyword
 |      argument.
 |  date_format : string, default None
 |      Format string for dates written into Excel files (e.g. 'YYYY-MM-DD')
 |  datetime_format : string, default None
 |      Format string for datetime objects written into Excel files
 |      (e.g. 'YYYY-MM-DD HH:MM:SS')
 |

In other words, the second parameter in order is the engine. So if you put a String without any detonation, it is considered as the engine (despite the note on the help about passing this parameter only as keyword, seems that this is the behaviour). If you enter again engine='openpyxl', then you are defining the parameter 'engine' twice.

This is the cause for error

TypeError: __new__() got multiple values for argument 'engine'

In summary, you should call ExcelWriter only with two parameters. The first one is the path of your Excel file (variable 'path', I guess) and the engine.



来源:https://stackoverflow.com/questions/49413824/modifying-an-existing-excel-workbooks-multiple-worksheets-based-on-pandas-dataf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!