首页 > 解决方案 > 将 Pandas DataFrame 保存为没有 pdfkit 的 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 个包和方法,但无法解决。

还有更多我可以尝试的软件包吗?这让我得了癌症

提前致谢。

标签: pythonpandasdataframepdf

解决方案


一种选择是从以下开始:

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 上进行了测试,结果是肯定的。


推荐阅读