ChrisChris89
ChrisChris89

Reputation: 61

String variable in Xlsxwriter Python

I am kind of new to python, and I was making a program with Tkinter that would let me write things in another excel file without actually having to open the Excel file.

From tkinter, I added an Entry box that I would have to write a value, and a button, that would use xlsxwriter to write the value inside the excel format.

I have tried

    name_entry = StringVar()
    name_get = name_entry.get()
    e1 = Entry(root, textvariable = name_entry)
    b1 = Button(root, text = "Create")
    e1.pack()
    b1.bind("<Button-1>", create)
    b1.pack()

the function 'create' is

    def create(event):
        workbook = xlsxwriter.Workbook(0, "2016" + str(name_get))
        worksheet = workbook.add_worksheet()
        worksheet.write(0, 0, 'name')
        worksheet.write(1, 0, str(name_get))
        workbook.close

When I run the program and write something in the entry and press the button(b1), the excel file that it creates only has the 'name' written in row = 0 and column = 0, but I don't have the name I wrote in the entry box in row = 1 and column = 0. Also, the name of the created file will only be saved as "2016", not "2016" + str(name_get), the value that I had written in the entry box.

Other than that, the program does not have any errors

How can I have the value written in the entry box to be written in the excel file?

Also, is it possible to create an extension of an excel file (.csv file, for example) in xlsxwriter?

Thank you so much for reading my question, and I would truly appreciate some advice!

Upvotes: 4

Views: 2086

Answers (2)

Noah Huntington
Noah Huntington

Reputation: 429

If I understand correctly you are trying to pass a string variable to the worksheet.write method.

This can be accomplished using the worksheet.write_string() method.

http://xlsxwriter.readthedocs.io/worksheet.html?highlight=write_string#write_string

ex. -

name_entry = ''
worksheet.write_string(1,0, name_entry)

Upvotes: 0

jmcnamara
jmcnamara

Reputation: 41644

There may be other issues but this line isn't correct:

   workbook = xlsxwriter.Workbook(0, "2016" + str(name_get))

The Workbook constructor takes the parameters: Workbook(filename[, options])

So that line should probably be:

   workbook = xlsxwriter.Workbook("2016" + str(name_get))

Also, is it possible to create an extension of an excel file (.csv file, for example) in xlsxwriter?

No.

XlsxWriter only creates xlsx files and the file extension on the output file should be .xlsx or Excel will complain when it opens the file.

Upvotes: 0

Related Questions