I am trying to use chrome headless to print to pdf. I am using the windows 10 Command Prompt. If I do the following, everything works as expected:
cd "C:\Program Files (x86)\Google\Chrome\Application"
chrome --headless --print-to-pdf=c:\Users\timmc\Documents\a.pdf --disable-gpu https://www.google.com/
However, ultimately I want to run this command from a python script using subprocess.call() and the spaces in 'Program Files (x86)' seem to be causing trouble. I have looked at other answers on stack overflow but not found anything that works. If I try the following:
C:\"Program Files (x86)"\Google\Chrome\Application\chrome --headless --print-to-pdf=c:\Users\timmc\Documents\b.pdf --disable-gpu https://www.google.com/
I get some very unusual behaviour where, google opens but not in headless mode, it opens two tabs, one with google, and one trying to open something like program%20--original-process-start-time%3D13156438844432514%20--fast-start%20files%20%28x86%29.
Can anyone explain the above behaviour?
Is there an easy way to deal with the spaces, that will work within a python script using subprocess.call()?
Is there an alternative way to achieve the same thing? (I'd rather not use selenium, but use chrome headless directly).
EDIT: The code that I am ultimately hoping to run from my python script is:
subprocess.call('C:\"Program Files (x86)"\Google\Chrome\Application\chrome --headless --print-to-pdf=c:\Users\timmc\Documents\b.pdf --disable-gpu https://www.google.com/',shell=True)
I don't run Windows, so I can't do any testing for you, but I can probably point you in the right direction.
Firstly, its a better practice to use a list of parameters to call
, instead of a big string. For example subprocess.call(["echo", "one", "two"])
instead of subprocess.call("echo", "one two")
. By doing this, you don't have to worry about quoting as much, since each parameter to .call
should be interpreted as a single parameter to chrome
, even if they contain spaces.
Also, there is some info at the bottom of the docs about how command line string interpretation happens in Windows: subprocess.call(["ls", "-l"])
17.1.5.1. Converting an argument sequence to a string on Windows On Windows, an args sequence is converted to a string that can be parsed using the following rules (which correspond to the rules used by the MS C runtime):
Arguments are delimited by white space, which is either a space or a tab. A string surrounded by double quotation marks is interpreted as a single argument, regardless of white space contained within. A quoted string can be embedded in an argument. A double quotation mark preceded by a backslash is interpreted as a literal double quotation mark. Backslashes are interpreted literally, unless they immediately precede a double quotation mark. If backslashes immediately precede a double quotation mark, every pair of backslashes is interpreted as a literal backslash. If the number of backslashes is odd, the last backslash escapes the next double quotation mark as described in rule 3.
https://docs.python.org/2/library/subprocess.html#converting-argument-sequence
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.