python - 在没有 pdfkit 的情况下将 Pandas DataFrame 保存为 PDF 文件格式

标签 python pandas dataframe pdf

我想将 pandas dataframe 保存为 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 to pdf 获得了第二位代码,并在 Mac OSX 上进行了测试,结果良好。

关于python - 在没有 pdfkit 的情况下将 Pandas DataFrame 保存为 PDF 文件格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51973991/

相关文章:

python - Python 中的回溯

python - 从另一个文件覆盖函数中的变量

python - Pandas 链索引的替代方案是什么?

python - 将列转换为 Pandas 数据框中的多行

Python在数据框中扩展网络地址

python - 在 Python Pandas 中根据描述场景和状态过滤产品

python - 如何将 UTC 时间戳字符串转换为 pandas 日期时间?

python - 如何从 Django Headers 获取数据?

python - 用积分约束解决四阶边值问题,python?

python - 如何计算 Pandas 中最长的不间断序列