我尝试在我的Windows 8机器上安装pdfkit Python API。我遇到了与路径有关的问题。
Traceback (most recent call last):
File "C:\Python27\pdfcre", line 13, in <module>
pdfkit.from_url('http://google.com', 'out.pdf')
File "C:\Python27\lib\site-packages\pdfkit\api.py", line 22, in from_url
configuration=configuration)
File "C:\Python27\lib\site-packages\pdfkit\pdfkit.py", line 38, in __init__
self.configuration = (Configuration() if configuration is None
File "C:\Python27\lib\site-packages\pdfkit\configuration.py", line 27, in __init__
'https://github.com/JazzCore/python-pdfkit/wiki/Installing-wkhtmltopdf' % self.wkhtmltopdf)
IOError: No wkhtmltopdf executable found: ""
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
是否有人在Windows机器上安装了Python PDFKIt?如何解决此错误。
我的示例代码:
import pdfkit
import os
config = pdfkit.configuration(wkhtmltopdf='C:\\Python27\\wkhtmltopdf\bin\\wkhtmltopdf.exe')
pdfkit.from_url('http://google.com', 'out.pdf')
以下应该可以在不修改Windows环境变量的情况下工作:
import pdfkit
path_wkthmltopdf = r'C:\Python27\wkhtmltopdf\bin\wkhtmltopdf.exe'
config = pdfkit.configuration(wkhtmltopdf=path_wkthmltopdf)
pdfkit.from_url("http://google.com", "out.pdf", configuration=config)
假设路径当然是正确的(例如在我的情况下它是r'C:\ Program Files(x86)\ wkhtmltopdf \ bin \ wkhtmltopdf.exe')。
请使用安装wkhtmltopdf,
sudo apt-get install wkhtmltopdf
对于Windows机器从下面的链接安装它,http://wkhtmltopdf.org/downloads.html
并且您需要将wkhtmltopdf
路径添加到环境变量中
我今天正在学习python,我遇到了同样的问题,最近我设置了windows环境变量,一切都还可以。 我将wkhtml的安装路径添加到路径中,例如:“D:\ developAssistTools \ wkhtmltopdf \ bin;”是我的wkhtml的安装路径,我把它添加到路径,一切都OK。
import pdfkit
pdfkit.from_url("http://google.com", "out.pdf")
最后,我发现了一个out.pdf。
IOError: 'No wkhtmltopdf executable found'
确保您的$ PATH中有wkhtmltopdf或通过自定义配置设置。 Windows中的where wkhtmltopdf
或Linux上的which wkhtmltopdf
应返回二进制的实际路径。
添加此配置行对我有用:
config = pdfkit.configuration(wkhtmltopdf="C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe")
pdfkit.from_string(html, 'MyPDF.pdf', configuration=config)
似乎你需要通过configuration=config
作为参数。
你需要设定
pdfkit.from_url('http://google.com','out.pdf',configuration = config)
发现Windows平台上的解码需要是二进制字符串,请尝试:
path_wkthmltopdf = b'C:\Program Files\wkhtmltopdf\\bin\wkhtmltopdf.exe'
config = pdfkit.configuration(wkhtmltopdf=path_wkthmltopdf)
pdfkit.from_url(url=urlpath, output_path=pdffilepath,configuration=config)
def urltopdf(url,pdffile): import pdfkit ''' input - url : target url - pdffile : target pdf file name ''' path_wkthmltopdf = 'D:\\Program Files (x86)\\wkhtmltopdf\\bin\\wkhtmltopdf.exe' config = pdfkit.configuration(wkhtmltopdf=path_wkthmltopdf) #pdfkit.from_url(url=urlpath, output_path=pdffilepath,configuration=config) pdfkit.from_url(url,pdffile,configuration=config) urltopdf('http://www.google.com','pdf/google.pdf')
很好的解决方案!感谢大家!
当我尝试上述所有方法时,我一直面临Permission Error,因为我没有工作站的管理员权限。如果您也是这种情况,请确保在安装wkhtmltopdf.exe时。安装的目标文件夹位于python site-packages文件夹中,或将目录添加到sys.path。通常它会安装在Program files文件夹中。我更改了安装目录,这对我有用:
import pdfkit
pdfkit.from_url("http://google.com", "out.pdf")
无需将wkhtmltopdf路径写入代码。只需为此定义一个环境变量,它就可以工作。
import pdfkit
pdfkit.from_url('http://google.com', 'out.pdf')
对我来说这个代码是有效的。