python 打开excel并在屏幕上呈现_如何用Python打开Excel文件显示其内容?
要在默認應用程序中打開文件,可以使用import os
file = "C:\\Documents\\file.txt"
os.startfile(file)
這將在與文件擴展名關聯的任何應用程序中打開文件。
但是也有一些缺點,所以如果你想對文件做一些更高級的處理(比如稍后關閉),你需要一個更高級的方法。您可以嘗試my question here的解決方案,該解決方案顯示如何使用subprocess.popen()跟蹤文件,然后關閉它。總的來說:>>> 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
>>>
這將保留您作為doc對象打開的文件,以便以后可以輕松關閉它
總結
以上是生活随笔為你收集整理的python 打开excel并在屏幕上呈现_如何用Python打开Excel文件显示其内容?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 菜谱分享网站微信小程序开发说明(2)-数
- 下一篇: 研修国学请注意选好教材