0

I have a dataframe called results and an excel file named as vlpandas.xlsx. I set a default path for working dir as follows: excel_dir = 'Users/Documents/Pythonfiles'

Based on the example from this post,: How to save a new sheet in an existing excel file, using Pandas?, I did the following:

book = load_workbook(excel_dir)

But I an error above after running the command, to fix it I use the usage as documented from the openpyxl and the following works:

book = load_workbook(filename = 'vlpandas.xlsx')

But then I get an exception error when I run the command below. Something about workbook.py from the openpyxl directory.

writer = pd.ExcelWriter(excel_dir, engine='openpyxl')

and then I want to complete my task saving the data to the new worksheet in the existing file vlpandas.xlsx with the following lines of code:

writer.sheets = dict((ws.title, ws) for ws in book.worksheets)    

## Your dataframe to append. 
results.to_excel(writer, '3rd_sheet')  

writer.save() 

So my two questions are:

1) What is the proper usage of load_Workbook from openpyxl 2) Why I am I getting an error with the command line below. I also changed my working folder path using the command osc.chdir:

writer = pd.ExcelWriter(excel_dir, engine='openpyxl')

Regards, Gus

jmcnamara
  • 38,196
  • 6
  • 90
  • 108
GusG
  • 363
  • 2
  • 4
  • 11
  • Why do you need to use openpyxl? Sorry if that's evident, I didn't get that – nahusznaj Jun 18 '18 at 08:11
  • 1
    The stack overflow link I provided at the top, posed a similar question and for saving to an existing file without overwriting an existing worksheets, they recommend to use openpyxl instead of ExcelWriter. When you use xlsxwriter to an existing file, it erases the file and then writes the new data. – GusG Jun 18 '18 at 23:05
  • I have since resolved my issue. – GusG Jun 23 '18 at 04:09

0 Answers0