问题
I have the standard pdf link made to open a file to a specific ppage, but if the file is already open, the page doesn't change, the script just opens the already open file, how can I navigate within an open file? or as a workaround, notice that the file is open, close it, and the reopen it?
path_to_pdf = os.path.abspath(openfile)
path_to_acrobat = os.path.abspath('C:\Program Files (x86)\Adobe\Acrobat 10.0\Acrobat\Acrobat.exe')
process = subprocess.Popen([path_to_acrobat, '/A', 'page=' + page, path_to_pdf], shell=False, stdout=subprocess.PIPE)
process.wait()
回答1:
Sometimes it's not polite to kill other processes -- especially if your program is not the one that spawned it. In such a case you may prefer to simply launch another acrobat process. To do that on Windows, use the /n option:
process = subprocess.Popen([path_to_acrobat, '/n', '/A', 'page=' + page, path_to_pdf], shell=False, stdout=subprocess.PIPE)
回答2:
A PDF is static printed content, so in short you can't. You'd have to regenerate the pdf or handle incoming requests on the receiving side.
Possibly print the pdf with a key in the link and redirect from a landing page based on that key.
回答3:
You may close the Acrobat instance (you may find the process using these approaches) and re-open the file at specific page
import psutils
path_to_pdf = os.path.abspath(openfile)
path_to_acrobat = os.path.abspath('C:\Program Files (x86)\Adobe\Acrobat 10.0\Acrobat\Acrobat.exe')
procs = [p for p in psutil.get_process_list() if p.name == 'Acrobat'] # you may be more selective here (e.g. per-user filter)
for proc in procs:
proc.terminate()
gone, alive = wait_procs(procs, 3)
for proc in alive:
proc.kill() # using brute force here
process = subprocess.Popen([path_to_acrobat, '/A', 'page=' + page, path_to_pdf], shell=False, stdout=subprocess.PIPE)
process.wait()
回答4:
Proper integration would require to remotely control acrobat - which is documented here : http://livedocs.adobe.com/acrobat_sdk/10/Acrobat10_HTMLHelp/wwhelp/wwhimpl/js/html/wwhelp.htm?&accessible=true
OLE automation can be done in python, there is even already an answer about that on SO : How do I script an OLE component using Python?
But for sure it's not a quick hack, you'll have to invest some time in learning windows-specific automation mechanisms and acrobat's API. But IMHO that's the most proper way to do it. And while I'm a big python supporter I would still think twice before using python in that case. I'm pretty sure more microsoft-specific tools and languages will allow you to solve this problem more quickly. What you want to do is not portable anyway.
来源:https://stackoverflow.com/questions/20869584/how-to-change-page-of-already-open-pdf-in-python