使用Python中的pdfkit将多个html文件转换为pdf

时间:2017-11-16 11:26:36

标签: python python-3.x pdf web-scraping

我尝试使用pdfkik将多个html文件转换为pdf。这是我的代码:

from bs4 import BeautifulSoup
from selenium import webdriver
import pdfkit

driver=webdriver.Chrome()
driver.get('https://www.linkedin.com/in/jaypratappandey/')
time.sleep(40)
soup= BeautifulSoup(driver.page_source, 'lxml')
data=[]
f=open('htmlfile.html', 'w')
top=open('tophtmlfile.html', 'w')

for name in soup.select('.pv-top-card-section__body'):
    top.write("%s" % name)

for item in soup.select('.pv-oc.ember-view'):
    f.write("%s" % item)


pdfkit.from_file(['tophtmlfile.html', 'htmlfile.html'], 'jayprofile.pdf')

driver.quit()

此代码出现以下错误:

Traceback (most recent call last):
  File "lkdndata.py", line 23, in <module>
    pdfkit.from_file(['tophtmlfile.html', 'htmlfile.html'], 'ankurprofile.pdf')
  File "/usr/local/lib/python3.5/dist-packages/pdfkit/api.py", line 49, in from_file
    return r.to_pdf(output_path)
  File "/usr/local/lib/python3.5/dist-packages/pdfkit/pdfkit.py", line 156, in to_pdf
    raise IOError('wkhtmltopdf reported an error:\n' + stderr)
OSError: wkhtmltopdf reported an error:
Error: This version of wkhtmltopdf is build against an unpatched version of QT, and does not support more then one input document.
Exit with code 1, due to unknown error.

1 个答案:

答案 0 :(得分:0)

我有同样的错误。您可能遇到的错误是由于您的qt安装不兼容和兼容qt版本的不可用性。 尝试运行

public class AO_Alarms_4 : AO_Alarms_3
{

    [SetUp]
    public void DefaultSetUp(){ }


    [Test]
    public void Alltest_Clases()
    {
        Alltest_2();
        Alltest_1();
    }

    [TearDown]

    public void DefaultTearDown() { }
}

[TestFixture]
[Parallelizable]
public class AO_Alarms_3 : AO_Alarms_2
{
    public WebDriverFactory driver = new WebDriverFactory(Utils.Browser);

    [SetUp]
    public void login_2()
    {
       //code
    }

    [Test]
    [Parallelizable]
    public void Alltest_2()
    {
        test_2();
    }

    public void test_2()
    {
       //code
    }

    [TearDown]

    public void teardown_2()
    {
       //code
    }
}

[TestFixture]
[Parallelizable]
public class AO_Alarms_2
{
    public WebDriverFactory driver = new WebDriverFactory(Utils.Browser);

    [SetUp]
    public void login_1()
    {
      //code
    }

    [Test]
    [Parallelizable]
    public void Alltest_1()
    {
        test1_1();
        test1_2();
    }

    [Test]
    public void test1_1()
    {
     //code
    }
    [Test]
    public void test1_2()
    {
    //code
    }

    [TearDown]
    public void teardown_1()
    {
        //code
    }
}
终端上的

,看看你是否能找到“降低功能”。

如果是,那么我的假设是正确的,那么你最安全的赌注就是从源头编译它。