python打开excel文件-如何使用Python打开Excel文件以显示其内容?

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.

解决方案

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值