3

I'm trying to open a PDF to a specific bookmark using python.

So far, I'm able to run the following command in Command Prompt and get exactly what I want (last is the name of a named destination inside the PDF test.pdf)

"C:\Program Files (x86)\Adobe\Reader 11.0\Reader\AcroRd32.exe" /A "nameddest=last" "C:\Users\User\Desktop\test.pdf"

But when I go to Python and try using the subprocess module like this:

import subprocess
subprocess.call(['"C:\Program Files (x86)\Adobe\Reader 11.0\Reader\AcroRd32.exe" /A "nameddest=last" "C:\\Users\\User\Desktop\test.pdf"'], shell=True)

I get "The filename, directory name, or volume label syntax is incorrect." Something I tried before this caused Adobe Reader to open, but give me a dialog box that had the same message

Why does what happens changes when I run it in python? And how can I fix it?


I'm running Anaconda 2.1.0 on Windows 8 and using Acrobat 10 to create Destinations. I've played around with python for 2-3 years, but don't know much more than someone with 1 semester of an intro programming class.

martineau
  • 119,623
  • 25
  • 170
  • 301
Frank Noam
  • 111
  • 7
  • Maybe this will help you: http://stackoverflow.com/questions/20869584/how-to-change-page-of-already-open-pdf-in-python, it shows you how to open a pdf file on a specific page – Thomas Wagenaar May 17 '15 at 16:41

1 Answers1

3

FIXED:

I'm an idiot.

"C:\\Users\\User\Desktop\test.pdf"

is an invalid path because the \t in \test.pdf is interpretted as a tab... It works after adding changing it to \test.pdf.

Code for anyone with the same problem later:

import os
import subprocess

page = "3"  
path_to_pdf = os.path.abspath("C:\\Users\\User\Desktop\\test.pdf")
path_to_acrobat = os.path.abspath('C:\Program Files (x86)\Adobe\Reader 11.0\Reader\AcroRd32.exe')


process = subprocess.Popen([path_to_acrobat, '/n', '/A', 'page=' + page, path_to_pdf], shell=False, stdout=subprocess.PIPE)

process.wait()
Frank Noam
  • 111
  • 7
  • You also can use `r"C:\Users\User\Desktop\test.pdf"`. Flag `r` at the head of string is raw string in python. – Hiadore Mar 06 '19 at 13:35