In WeasyPrint’s public API I accept filenames (among other types) for the HTML inputs. Any filename that works with the built-in open()
should work, but I need
Does the following work for you?
from urlparse import urlparse, urlunparse
urlunparse(urlparse('yourURL')._replace(scheme='file'))
Credit to comment from @danodonovan
above.
For Python3, the following code will work:
from urllib.parse import urljoin
from urllib.request import pathname2url
def path2url(path):
return urljoin('file:', pathname2url(path))
For completeness, in Python 3.4+, you should do:
import pathlib
pathlib.Path(absolute_path_string).as_uri()
I'm not sure the docs are rigorous enough to guarantee it, but I think this works in practice:
import urlparse, urllib
def path2url(path):
return urlparse.urljoin(
'file:', urllib.pathname2url(path))