quinn
quinn

Reputation: 31

xlsxwriter, openpyxl: 'Workbook' object has no attribute 'write'

I am trying to create an excel report with nice formatting from my Pandas dataframes. Initially, I was overwriting my headers with the Pandas file. Thus, I wrote the formatting code first, closed the workbook, and loaded it again with openpyxl to write my pandas dataframe onto the existing worksheet.

However, I ran into this error when I tried to writer.save():

AttributeError: 'Workbook' object has no attribute 'write'

This is my code:

output_filename = "".join(org_name.split()) + OUTPUT_FILENAME 

workbook = xlsxwriter.Workbook(output_filename)   
worksheet = workbook.add_worksheet()

# insert logo
worksheet.set_column('A:A', 30)
worksheet.insert_image('A1', picture, {'x_scale': 0.7, 'y_scale': 0.7})

# add titles: 
title_format = workbook.add_format({'bold': True})
worksheet.write('A3', 'Report', title_format)
worksheet.write('A5', org_name, title_format)
worksheet.write('A6', 'Report Year', title_format)
worksheet.write('A7', 'Report Month', title_format)
worksheet.write('B6', THIS_YEAR)
worksheet.write('B7', calendar.month_name[THIS_MONTH])

workbook.close()



df = get_organization_df(all_df, org_name)

book = load_workbook(output_filename)

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

# get user counts
users_df = count_users(df)
users_df.to_excel(writer, sheet_name = 'Sheet1', startrow = 8)

writer.save()

Thanks in advance for your help!

Upvotes: 2

Views: 9494

Answers (1)

Jimmy Lee Jones
Jimmy Lee Jones

Reputation: 935

I beleive your problem is here: writer = pd.ExcelWriter(book, engine='openpyxl'): book is a Workbook, while the parameter of pd.ExcelWriter is a string

Upvotes: 1

Related Questions