我尝试使用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
我已从Here下载了wkhtmktopdf并已安装。添加了环境变量的路径,但仍显示相同的错误。
我尝试过配置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)
如何解决这个问题?
答案 0 :(得分:7)
您的配置路径包含ASCII Backspace,\b
中的\bin
,其中pdfkit似乎正在剥离并将C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe
转换为C:\Program Files\wkhtmltopdf\wkhtmltopdf.exe
。
可以使用r
来解决此问题,这会使其成为raw literal
config_path = r'C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe'
或\\
config_path = 'C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe'