Python 打开pdf文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19453338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 13:46:45  来源:igfitidea点击:

Opening pdf file

python

提问by Aleksa

I wanna open pdf file from python console, I can do it with os.system(filename), it will open in adobe reader, but the problem is that os.systemalso opens a command prompt, is there another way that won't open command prompt?

我想从 python 控制台打开 pdf 文件,我可以用os.system(filename),它会在 adobe reader 中打开,但问题是os.system也打开了命令提示符,还有另一种方法不会打开命令提示符吗?

采纳答案by Daniel W?rn?

Try:

尝试:

subprocess.Popen([file],shell=True)

回答by breakbadjames

Read the documentation thoroughly. The very first line of the os.system method is:

仔细阅读文档。os.system 方法的第一行是:

Execute the command (a string) in a subshell.

在子shell中执行命令(一个字符串)。

Knowing this, you can now seek alternate solutions, such as the subprocess module already mentioned.

知道了这一点,您现在可以寻求替代解决方案,例如已经提到的 subprocess 模块。

回答by Static Void

import webbrowser
webbrowser.open_new(r'file://C:\path\to\file.pdf')

回答by Astrophe

This is a bit late but nobody mentioned:

这有点晚了,但没有人提到:

open("file_name.pdf")

回答by Raj Stha

import os
os.startfile(filename)