不使用pdfkit将Pandas DataFrame保存为PDF文件格式


问题内容

我想将熊猫数据框保存为pdf格式。

import pdfkit as pdf    
config = pdf.configuration(wkhtmltopdf="C:\Program Files\wkhtmltopdin\wkhtmltopdf.exe")
    pdf.from_url('http://google.com', 'out.pdf',configuration=config)
--> not working somehow even though I downloaded wkhtmltopdin on several different locations

from weasyprint import HTML
HTML(string=pd.read_csv('cor.csv').to_html()).write_pdf("report.pdf")

dlopen() failed to load a library: cairo / cairo-2 / cairo-gobject-2
--> not working : Tried several times to solve this isseue, but cannot download library

我在stackoverflow和其他网站中尝试了5个以上的软件包和方法,但无法解决。

还有更多可以尝试的软件包吗?这给我带来了癌症

提前致谢。


问题答案:

一种选择是从以下开始:

df.to_html()

然后使用QT将HTML转换为PDF,如下所示:

from PyQt4.QtGui import QTextDocument, QPrinter, QApplication

import sys
app = QApplication(sys.argv)

doc = QTextDocument()
location = "c://apython//Jim//html//notes.html"
html = open(location).read()
doc.setHtml(html)

printer = QPrinter()
printer.setOutputFileName("foo.pdf")
printer.setOutputFormat(QPrinter.PdfFormat)
printer.setPageSize(QPrinter.A4)
printer.setPageMargins(15, 15, 15, 15, QPrinter.Millimeter)

doc.print_(printer)
print("done!")

我获得了从html到pdf的第二部分代码,并在Mac OSX上进行了测试,取得了积极的成果。