python
  • python-3.x
  • wkhtmltopdf
  • pdfkit
  • 2017-02-23 56 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' 
    
    相關問題