Search code examples
pythonpython-3.xpandaspython-requestspandas.excelwriter

Issues with saving an Excel.Writer file to new path


I am trying to create a timed backup system for my excel document with Python as multiple users will be accessing it. I want to change the path of the file not to my local directory.

Here's the code;

import pandas as pd
import datetime
import numpy
now = datetime.datetime.now()
ct = now.strftime("%Y-%m-%d %H.%M")

table = pd.read_excel(r'Z:\new\Planner_New.xlsx',
                  sheet_name = 'Jan18',                
                  header = 0,
                  index_col = 0,
                  usecols = "A:AY",
                  convert_float = True)

writer = pd.ExcelWriter('Planner' + ct + '.xlsx', engine='xlsxwriter')
table.to_excel(writer, sheet_name = "Jan18")

workbook  = writer.book
worksheet = writer.sheets['Jan18']

format1 = workbook.add_format({'num_format': '0%'})
worksheet.set_column('H:AY', None, format1)

writer.save()
writer.close()

I have tried

outpath = (r'Z:\backup')
writer.save(outpath)
writer.close()

But get back

TypeError: save() takes 1 positional argument but 2 were given


Solution

  • You need to specify the save location when you create the ExcelWriter object:

    writer = pd.ExcelWriter(r'Z:\backup\Planner' + ct + '.xlsx', engine='xlsxwriter')
    
    ...
    
    writer.save()
    writer.close()