python
  • python-3.x
  • wkhtmltopdf
  • pdfkit
  • 2017-02-23 101 views 3 likes 
    3

    我想一个网页转换为PDF格式,使用pdfkit但它显示了以下错误Pdfkit OSERROR:没有wkhtmltopdf可执行发现

    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) 
    

    如何解决这个问题?

    +3

    在'\ bin'的'\ B'是[ASCII退格](HTTP ://stackoverflow.com/questions/25065608/what-does-backward-slash-b-do-in-python)。尝试'r“C:\ Program Files \ ...”或“C:\\ Program Files \\ ...”。 – Wondercricket

    +0

    哦!非常感谢 !!有效 !! :D发布它作为答案。 @Wondercricket –

    回答

    3

    你的配置路径包含ASCII Backspace,该\b\bin,其中pdfkit似乎被剥离出来,转换C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exeC:\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' 
    
    相关问题