telex-wap
telex-wap

Reputation: 852

How to get the binary contents of a pandas xlsxwriter object for dropbox upload

I am trying to upload to dropbox an Excel sheet with multiple tabs made with pandas and xlsxwriter. Not very big files, just around 5MB. I am using the dropbox module for Python 3

I am doing:

filename = pd.ExcelWriter('myfile.xlsx' ,engine='xlsxwriter')
actions.to_excel(filename, sheet_name="Combined Actions")
filename.save()

with open(filename, "rb") as f:
    dbx.files_upload(f.read(), "/MyAPP/"+filename)

I read in the docs that files_upload expects a bytes object. I am getting the error: Type Error: Invalid file: <pandas.io.excel__XlsxWriter object at 0x000000000071C48>

Is this error because I am not supplying the file in the needed format? How can I do this upload so I don't get a type error?

Upvotes: 1

Views: 1530

Answers (1)

alxwrd
alxwrd

Reputation: 2470

You are trying to use the built in function open to open an object. It requires a string as its first argument (which represents a file path). More information on the open function can be found in the docs.

The source for pandas ExcelWriter shows it stores the filename you pass to it in .path. So renaming your variables to better represent what they are and using the .path attribute of the ExcelWriter instance:

excel_file = pd.ExcelWriter('myfile.xlsx', engine='xlsxwriter')
actions.to_excel(excel_file, sheet_name="Combined Actions")
excel_file.save()

with open(excel_file.path, 'rb') as f:
    dbx.files_upload(f.read(), '/MyAPP/'+excel_file.path)

Upvotes: 3

Related Questions