Reputation: 5715
I am using a hmtl file as a help document for my program, and would really like to be able to open the file at a specific point. i assumed i would be able to do this using the built in webbrowser module by specifying a url with a bookmark.
this is my html file name: help.html i assumed that i would be able to use: help.html#top
this is the code i am using to open the file, this works fine:
webbrowser.open("Files\help.html")
and this is the code i have been trying to use to open at a specific point which ie9 apparently cant display (not sure why it is trying to load in ie9 as chrome is my default browser, and the working one above loads in chrome):
webbrowser.open("Files\help.html#2.1.0")
any ideas guys?
Upvotes: 2
Views: 1871
Reputation: 513
As I wrote here, this works for me:
import sys
import subprocess
html_filepath = r'c:/path/to/files/html.html#2.1.0'
subprocess.check_call(
[sys.executable, '-m', 'webbrowser', html_filepath]
)
As you can see you do not need to prepend file://
.
Upvotes: 0
Reputation: 28686
webbrowser.open()
calls the browser from the command line. So you might try to do that yourself first. If that doesn't work, it's likely that your browser just doesn't support that for local files or something.
With Ubuntu+Firefox for example, webbrowser.open()
does what you ask. (but - as Dave Webb said in his answer - you do have to provide a file:
url, not just a filename).
(not on Windows at the moment, so haven't checked there)
As for why it doesn't load chrome but ie9: (you can look in the webbrowser.py code yourself if you want) I think it does try to use your default webbrowser, by doing os.startfile(url)
. What happens when you doubleclick your help.html file, of when you just type help.html
(adjust path as needed) at the command line? It should do the same.
EDIT:
It seems that it doesn't always use the command line. On Windows, when trying to use the default browser, it uses os.startfile()
which in turn uses the win32 ShellExecute
api. ShellExecute
can be used to perform certain actions on a file, folder or URL, like "open", "edit" or "print" with its default application. In this case, ShellExecute
is asked to "open" the URL.
It seems however, that ShellExecute
ignores the fragment identifier (the part after #
) when opening file:
urls. Strangely enough, this is not the case with http:
urls. Presumably, a file:
url is just converted to a plain filename first.
There seems to be little you can do about this except:
webbrowser
module, and use webbrowser.get()
to get your controller, see docs)BROWSER
environment variable (see webbrowser module docs)Or, the easiest way: as you seem to be on windows: just try to open internet explorer specifically:
try:
browser = webbrowser.get('c:\\Program Files\\Internet Explorer\\IEXPLORE.EXE')
except Webbrowser.Error:
browser = webbrowser.get()
browser.open(url)
(This will fall back to using the default, so your code would still work on other platforms)
Upvotes: 3
Reputation: 193714
I think webbrowser
is expecting a URL, so have you tried something like:
webbrowser.open("file://c:/path/to/files/html.html#2.1.0")
Upvotes: 1