bot_der_4
bot_der_4

Reputation: 67

Xlsxwriter Not Writing Into File

import xlsxwriter

workbook = xlsxwriter.Workbook('123.xlsx')
worksheet = workbook.add_worksheet()

worksheet.write(3, 3, 'Test')
workbook.close()

I am trying to add "Test" to an excel file using xlsxwriter. But by doing so it replaces the whole excel file with only the new added text.

It's the same result when I write it into a new file.

save() for some reason doesn't even get recognized (it's white).

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

Upvotes: 1

Views: 2627

Answers (1)

jahantaila
jahantaila

Reputation: 908

You forgot workbook.close(), add that to the end of your program, and it should work

The syntax to close an excel file in xlsxwriter is very simple i.e

workbook_object.close()

Here workbook object is the workbook which is open and now you want to close it. Its better to save the workbook before closing it. demo.xlsx can be any file name that you want.

workbook = xlsxwriter.Workbook('demo.xlsx')

So your final code will look like this:

import xlsxwriter

workbook = xlsxwriter.Workbook('123.xlsx')
worksheet = workbook.add_worksheet()

worksheet.write(3, 3, 'Test')
workbook = xlsxwriter.Workbook('demo.xlsx')
workbook.close()

EDIT:

Based on what you provided, your problem seems to lie with the workbook not properly closing.

import xlsxwriter

workbook = xlsxwriter.Workbook('test.xlsx')
worksheet = workbook.add_worksheet()


worksheet.write(3, 3, 'hope this works :)')  # Writes a string to the file



workbook.close()

Upvotes: 4

Related Questions