0

I have account names that are saved in a python list, let's say "account a", "account b", "account c" and so on. This list has been saved in a Pandas dataframe.

I'd like to have an excel file created for each of those accounts, but I want the file to have the name of the account that it represents.

My initial thought was to create a for loop and use xlsxwriter, something like this....

    with pd.ExcelWriter(workbook, engine = 'xlsxwriter') as writer: 
        account_list = ['account a', 'account b', 'account c']



for account in account_list: 

        workbook = xlsxwriter.Workbook([account_list])
        worksheet = workbook.add_worksheet([account_list])

        account_list.to_excel(writer, sheet_name = f'[worksheet]')

You can probably guess that I get an error, saying "expected string or bytes-like object."

Any thoughts on how to make this work?

2
  • From what I see with your code, you are trying to add individual worksheets named for the accounts in account_list to an excel file, not creating separate files. Which would you like to do, sheets within a file or separate files? Commented Jan 20, 2022 at 22:04
  • I would like to do both. A file with the account name, with the sheet in that file also being the account name. Commented Jan 21, 2022 at 2:55

1 Answer 1

1

Use the variable choosed in for loop:

import xlsxwriter

account_list = ['account a', 'account b', 'account c']

for account in account_list:

    workbook = xlsxwriter.Workbook( account + '.xlsx')  # the file name is given by account
    worksheet = workbook.add_worksheet(account) # if 'account' ommited, the sheet is named as 'sheet1'

    worksheet.write('A1', account)

    workbook.close()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.