Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python error: xlsxwriter.exceptions.FileCreateError: [Errno 2] No such file or directory: ''

What I am trying to do :

I am trying to save a file using filedialog. The saving part works well but if I click cancel on the dialog box choosing not to save, I get this error. How can this error be removed?

Error I am getting:

Exception in Tkinter callback

FileNotFoundError: [Errno 2] No such file or directory: ''

During handling of the above exception, another exception occurred:


xlsxwriter.exceptions.FileCreateError: [Errno 2] No such file or directory: ''

Below is my code :

import xlsxwriter
import tkinter as tk
import tkinter.ttk as ttk
from tkinter import filedialog

def save():
    filename = filedialog.asksaveasfilename(filetypes=[("Excel files", ".xlsx .xls")],defaultextension='.xlsx')
    workbook = xlsxwriter.Workbook(filename)
    worksheet = workbook.add_worksheet()
    worksheet.write('A1', 'Common Information') 
    worksheet.write('A2', 'Circumference of Tank')
    worksheet.write('C2', 'Height of Tank')
    worksheet.write('A3', 'Minimum Thickness of Tank')
    worksheet.write('C3', 'Maximum Thickness of Tank')   

    workbook.close()  

window = tk.Tk()
window.configure(background='white')

ws = window.winfo_screenwidth()
hs = window.winfo_screenheight()
w = 700 # width for the Tk root
h = 585  # height for the Tk root
x = (ws / 2) - (w / 2)
y = (hs / 2) - (h / 2)

window.geometry('%dx%d+%d+%d' % (w, h, x, y))   

canvas = tk.Canvas(window,bg="white",width=700, height=585, highlightthickness=0)
canvas.pack()


save1 = ttk.Button(canvas, text='Save', command= lambda: save())
canvas.create_window(15, 15, window=save1, anchor=tk.NW)


window.resizable(False, False)
window.mainloop()
like image 720
devinxxxd Avatar asked Nov 18 '25 08:11

devinxxxd


1 Answers

Your save function will be called after you press the button.

filename = filedialog.asksaveasfilename(filetypes=[("Excel files", ".xlsx .xls")],defaultextension='.xlsx')
....

Because when you stop selecting the path,the filename will be (it is a str type but its length == 0 ).And workbook = xlsxwriter.Workbook(filename) will raise Exception.

So you should use a if statement before you save your xlsx file.

And the save() function should be:

def save():
    filename = filedialog.asksaveasfilename(filetypes=[("Excel files", ".xlsx .xls")],defaultextension='.xlsx')
    if filename:
        workbook = xlsxwriter.Workbook(filename)
        worksheet = workbook.add_worksheet()
        worksheet.write('A1', 'Common Information')
        worksheet.write('A2', 'Circumference of Tank')
        worksheet.write('C2', 'Height of Tank')
        worksheet.write('A3', 'Minimum Thickness of Tank')
        worksheet.write('C3', 'Maximum Thickness of Tank')

        workbook.close()
like image 153
jizhihaoSAMA Avatar answered Nov 19 '25 21:11

jizhihaoSAMA



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!