O JOE
O JOE

Reputation: 597

Opening excel file with tkfiledialog

I have few code to open file in excel spreadsheet

filename = filedialog.askopenfilename(initialdir="C:/", title="select file",
                                          filetypes=(("excel files", "*.xls"), ("all files", "*.*")))

os.system(r"excel.exe" + filename)

when i select the file i want read i get the following error:

excel.exeC:' is not recognized as an internal or external command, operable program or batch file.

I tried to open with notepad wiht the os.system(r"notepad.exe" + filename) but i want to open it in excel using the excel. I welcome your suggestion to open it in excel file.

Upvotes: 1

Views: 7280

Answers (1)

Ethan Field
Ethan Field

Reputation: 4730

You can specifically refer to the excel .exe file, however, you would need to use the full location and this can be different from machine to machine depending on OS, whether they've done a custom install etc.

Instead, you can simply refer to the file itself and the OS should just launch the file in the computer's default program for the file type:

from tkinter import filedialog
import os

filename = filedialog.askopenfilename(initialdir="C:/", title="select file")

os.system(filename)

Additionally, the reason why you're getting that error is that when you're calling os.system(r"excel.exe" + filename) you're calling excel.exe[filename] without a space character.

This means that if you were trying to open, say, C:/excel.xls then you'd be calling excel.exeC:/excel.xls which isn't a valid command.

Upvotes: 1

Related Questions