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'
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.