Python - open pdf file to specific page/section

14,044

Here are two basic ideas

Case 1: you want to open the file in 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()

As for the section, you can have a look to this answer

Case 2: you want to call acrobat to open your file at a specific page

From this Acrobat help document, you can pass this to a subprocess:

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()

Just a suggestion: if you want to open the file at a specific section, you could use the parameter search=wordList where wordlist is a list of words seperated by spaces. The document will be opened and the search will be performed, the first result of it being highlighted. This way, as a wordlist, you can try to put the name of the section.

Share:
14,044
Das.Rot
Author by

Das.Rot

Engineer

Updated on June 11, 2022

Comments

  • Das.Rot
    Das.Rot almost 2 years

    Is it possible to open a pdf from within Python such that it goes to a specific page or section? What I am thinking is to have it open a help file (pdf) and jump to the section that the help is being requested for.