Home > other >  Opening pdf page
Opening pdf page

Time:09-05

I wanna open pdf page from python console, I can do it with import webbrowser , it will open in browser, but the problem is that it's not directly jumping to defined page. for this example it's page#41

import webbrowser
webbrowser.open_new(r'C:\test.pdf#page=41')

CodePudding user response:

#tags are URL parameter fragments for browser use, thus need to be passed to the target browser as a full command line argument.

Fragments cannot normally be used as simple command line FTA actions since shell cmd requires spacing between actions. Thus you can use
webbrowser.open_new(r'C:\test.pdf') but without the attached #page=41

You will need to hard code the browser with a combined URL and allow for quoting spaces in the browsers path (However if the default PDF enhanced default browser is on path, that could be shorter).

"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe" file:///c:/test.pdf#page=41

Note :- many other programable options may be available like zoom, search etc. however they vary greatly between browsers. (Edge/Chrome currently cannot search but Firefox can).

enter image description here

An alternative NOT recommended method may to avoid the need for a path to exe such as here

enter image description here

But safest is call any exe by its full path location to avoid any security problems.

CodePudding user response:

Please confirm the Google-Chrome location in your file directory

import webbrowser

# url = 'file:///C:/test.pdf#page=41'
url = 'file:///A:/ds/personal/Please_DocuSign_AlliedMedia_Confirmation_Let.pdf#page=3'

# Please confirm the Google-Chrome location in your file directory 

# MacOS
# chrome_path = 'open -a /Applications/Google\ Chrome.app %s'

# Windows
chrome_path = 'C:/Program Files/Google/Chrome/Application/chrome.exe %s'

# Linux
# chrome_path = '/usr/bin/google-chrome %s'


WB = webbrowser.get(chrome_path)
WB.open(url)
  • Related