Python - 打开 PDF 文件到特定页面/部分

7

是否有可能从Python中打开PDF,使其跳转到特定的页面或部分?我的想法是打开一个帮助文件(PDF),并跳转到请求帮助的部分。


2
你会使用什么来打开文件? - David Z
看一下pypdf。http://pybrary.net/pyPdf/ - Jack_of_All_Trades
我想使用默认的PDF阅读器打开文件。我非常确定这通常是Adobe Reader。也许有一种方法可以将Adobe Reader打开到特定的页面吗?我现在怀疑我必须通过Adobe Reader而不是Python来完成这个任务。我还可能需要将Python代码放在“try”块中,以防用户没有安装Adobe Reader。有人知道发送Adobe Reader以打开特定页面的参数吗? - Das.Rot
2
似乎在某个页面上没有打开Adobe Reader的参数:https://dev59.com/dHRB5IYBdhLWcg3wc280#619203 - dusan
1个回答

9

以下是两个基本思路

情况1:你想在Python中打开文件

from pyPdf import PdfFileReader, PageObject

pdf_toread = PdfFileReader(path_to_your_pdf)

# 1 is the number of the page
page_one = pdf_toread.getPage(1)

# This will dump the content (unicode string)
# According to the doc, the formatting is dependent on the
# structure of the document
print page_one.extractText()

关于这部分,可以参考这个答案 案例2:您想要调用Acrobat打开您的文件并跳转到特定页面
从这个Acrobat帮助文档中,您可以将以下内容传递给子进程:
import subprocess
import os

path_to_pdf = os.path.abspath('C:\test_file.pdf')
# I am testing this on my Windows Install machine
path_to_acrobat = os.path.abspath('C:\Program Files (x86)\Adobe\Reader 10.0\Reader\AcroRd32.exe') 

# this will open your document on page 12
process = subprocess.Popen([path_to_acrobat, '/A', 'page=12', path_to_pdf], shell=False, stdout=subprocess.PIPE)
process.wait()

建议:如果您想在特定部分打开文件,可以使用参数search=wordList,其中wordlist是由空格分隔的单词列表。文档将被打开并执行搜索,第一个结果将被突出显示。作为wordlist,您可以尝试放置章节名称。


不必在Python中打开和阅读PDF。我想用默认的PDF阅读器打开PDF并跳转到特定页面。 - Das.Rot
我遇到了一个问题。我的Acrobat Reader打开并显示“打开文档时出错。文件名、目录名或卷标语法不正确”。我无法找到解决方案,甚至无法确定错误所在。 - Dev_Man
print(pageObj.extractText()) 为什么会返回 None 或 ''? - Manuel Carrero

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接