6

I am trying to open an excel file with Python to display the data that contented in it, just like we double click it with mouse.

I've search for a while, but seems all the pages are talking about how to read and write an excel file with code, rather than display the content to the user.

So, is there any solution for my problem?

Thanks a lot.

2
  • The fact that it is an excel file in this case is irrelevant. You could just ask how to open any file with its default program, by file extension, using python. Commented Jan 17, 2014 at 17:43
  • It may be relevant as you can use COM for Office automation under Windows. Commented Jan 17, 2014 at 18:53

2 Answers 2

27

To simply open a file in its default application, you can use

import os
file = "C:\\Documents\\file.txt"
os.startfile(file)

This will open the file in whatever application is associated with the file extension.

There are some drawbacks however, so if you want to do some more advanced handling of the file (such as closing it later), you need a more advanced approach. You can try the solution to my question here which shows how to use subprocess.popen() to keep track of the file, and then close it. Here's the general idea:

>>> import psutil
>>> import subprocess
>>> doc = subprocess.Popen(["start", "/WAIT", "file.pdf"], shell=True)   #Stores the open file as doc
>>> doc.poll()                                                           #Shows that the process still exists (will return 0 if the /WAIT argument is excluded from previous line)
>>> psutil.Process(doc.pid).get_children()[0].kill()                     #Kills the process
>>> doc.poll()                                                           #Shows that the process has been killed
0
>>> 

This retains the file you opened as the doc object so that it can be easily closed later

Sign up to request clarification or add additional context in comments.

5 Comments

@Robᵩ, whoops, silly mistake, thanks for the catch! What does the r"" do, I've never seen it?
r'string' is a "raw" string; python doesn't interpret anything in it so you don't have to escape anything.
@GoingTharn, wow, I wish I knew about that a month ago... Thanks
I think everyone who discovers it thinks the same thing ;)
RE: raw strings -- a backslash will still escape a quote character (" or ') so you can't do r"C:\files\go\here\but\end\in\a\slash\" -- it will give you SyntaxError: EOL while scanning string literal
1

Just to complement wnnmaw's answer, subprocess.popen supports context management and the 'with' operator, which provides a nice, clean, Pythonic way to handle the opening and closing of the file. Basically, there's no need to explicitly close the file with this approach.

import psutil
import subprocess
with subprocess.Popen(["start", "/WAIT", "file.pdf"], shell=True) as doc:
    # use 'doc' here just as you would the file itself
    doc.poll()
    doStuff(doc)
    for line in readline(doc):
        etc, etc...

# then just continue with the rest of your code.

The 'with' statement will automatically handle the opening and closing for you, and it's nice and easy to read. The one caveat you must bear in mind is that this is only good for one file at a time. If your function is going to deal with multiple files at once you're better off handling the open/close manually.

If you just want to pass control over to Excel (relying on the end-user to close the file in Excel when finished), see the first part of wnnmaw's answer.

2 Comments

This is a nice extension of my answer, but you should make a note that with comes with the limitation of only being able process one file at a time. Say OP wants to open 5 files, then close 3, with can't handle that easily. That being said, this is definitely the way to go if only one file needs to go at a time
@wnnmaw, thanks for pointing that out! I've made an edit.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.