Pdfkit OSError: 找不到wkhtmltopdf可执行文件

7
我正在尝试使用pdfkit将网页转换为PDF,但出现以下错误。
Traceback (most recent call last):

  File "<ipython-input-39-33289a2ef087>", line 1, in <module>
runfile('H:/Python/Practice/pdf_read_write.py', wdir='H:/Python/Practice')

  File "C:\Program Files\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 866, in runfile
execfile(filename, namespace)

  File "C:\Program Files\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)

  File "H:/Python/Practice/pdf_read_write.py", line 10, in <module>
config = pdfkit.configuration(wkhtmltopdf="C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe")

  File "C:\Program Files\Anaconda3\lib\site-packages\pdfkit\api.py", line 83, in configuration
return Configuration(**kwargs)

  File "C:\Program Files\Anaconda3\lib\site-packages\pdfkit\configuration.py", line 27, in __init__
'https://github.com/JazzCore/python-pdfkit/wiki/Installing-wkhtmltopdf' % self.wkhtmltopdf)

OSError: No wkhtmltopdf executable found: "C:\Program Files\wkhtmltopdin\wkhtmltopdf.exe"
If this file exists please check that this process can read it. Otherwise please install wkhtmltopdf - https://github.com/JazzCore/python-pdfkit/wiki/Installing-wkhtmltopdf

我已经从这里下载并安装了wkhtmltopdf。将路径添加到环境变量,但仍显示相同的错误。
我已尝试配置pdfkit,但无法解决问题。
以下是我的代码:
import pdfkit
config = pdfkit.configuration(wkhtmltopdf="C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe")
pdfkit.from_url("http://www.geeksforgeeks.org/convex-hull-set-2-graham-scan/", "out.pdf",configuration=config)

如何解决这个问题?

3
\bin 中的 \bASCII 退格符 。尝试使用 r"C:\Program Files\...""C:\\Program Files\\..." - Wondercricket
哦!!谢谢!!它奏效了!!:D 把它作为答案发布。@Wondercricket - MD. Khairul Basar
1个回答

13
你的配置路径中包含一个ASCII退格符,即\bin中的\b,pdfkit似乎会将其剥离并将C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe转换为C:\Program Files\wkhtmltopdf\wkhtmltopdf.exe
可以通过使用r来解决此问题,这将使其成为一个原始字面量
config_path = r'C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe'

或者\\

config_path = 'C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe'

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接