为什么 Python 的模块操作系统不运行 chromehtml2pdf 命令?



当我运行命令时

chromehtml2pdf --out=E:Trialtest3.pdf https://www.wikiwand.com/en/Stochastic_process

在Windows命令提示符窗口中,它运行良好,并在文件夹E:Trial中生成文件test3.pdf,输出如下:

Converting file: https://www.wikiwand.com/en/Stochastic_process
out = E:Trialtest3.pdf

不幸的是,当我从 Python 调用此命令时

import os
cmd = "chromehtml2pdf --out=E:Trialtest3.pdf https://www.wikiwand.com/en/Stochastic_process"
os.system(cmd)

它不返回任何内容,即不生成 PDF 文件。

chromehtml2pdf的完整限定文件名为:

C:UsersDung LeAppDataRoamingnpmchromehtml2pdf.cmd

我也试过:

import subprocess
cmd = '"C:UsersDung LeAppDataRoamingnpmchromehtml2pdf.cmd" --out="E:Trialtest1.pdf" http://www.randomservices.org/random/foundations/Measure.html'
subprocess.run(cmd, shell = True)

但它返回错误:

File "<ipython-input-21-4339c7e1a889>", line 2
cmd = '"C:UsersDung LeAppDataRoamingnpmchromehtml2pdf.cmd" --out="E:Trialtest1.pdf" http://www.randomservices.org/random/foundations/Measure.html'
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 3-4: truncated UXXXXXXXX escape.

我不知道有什么问题:

cmd = '"C:UsersDung LeAppDataRoamingnpmchromehtml2pdf.cmd" --out="E:Trialtest1.pdf" http://www.randomservices.org/random/foundations/Measure.html'

您能否详细说明此问题以及如何解决?

@Mofu和@HackingAddict1337的评论解决了我的问题。非常感谢!


import subprocess
cmd = r'"C:UsersDung LeAppDataRoamingnpmchromehtml2pdf.cmd" --out="E:Trialtest1.pdf" http://www.randomservices.org/random/foundations/Measure.html'
subprocess.run(cmd, shell = True)

最新更新