无需 pdfkit 即可将 Pandas DataFrame 保存为 PDF 文件格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51973991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 05:58:11  来源:igfitidea点击:

Saving Pandas DataFrame into PDF File format without pdfkit

pythonpandasdataframepdf

提问by Kevin Choi

I want to save a pandas dataframe into pdf format.

我想将Pandas数据框保存为 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

I have tried 5 more packages and methods in stackoverflow and other websites but could not solve it.

我在 stackoverflow 和其他网站上尝试了另外 5 个包和方法,但无法解决。

Is there any more packages that I can try more? this giving me a cancer

有没有更多的包我可以尝试更多?这让我得了癌症

Thanks in advance.

提前致谢。

回答by Philip DiSarro

One option is to start with:

一种选择是开始:

df.to_html()

and then use QT to convert the HTML to PDF as follows:

然后使用 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!")

I obtained the second bit of code from html to pdf, and tested on Mac OSX with positive results.

我获得了从html 到 pdf的第二位代码,并在 Mac OSX 上进行了测试,结果是肯定的。

回答by AJ Koenig

Have you considered drawing a Matplotlib Table, then exporting the Table Figure?

您是否考虑过绘制 Matplotlib 表格,然后导出表格图形?

import matplotlib.backends.backend_pdf
import matplotlib.pyplot as plt
import pandas as pd

d = {'x{}'.format(i): range(30) for i in range(10)}

table = pd.DataFrame(d)

fig = plt.figure()

ax=fig.add_subplot(111)

cell_text = []
for row in range(len(table)):
    cell_text.append(table.iloc[row])

ax.table(cellText=cell_text, colLabels=table.columns, loc='center')
ax.axis('off')

pdf = matplotlib.backends.backend_pdf.PdfPages("output.pdf")
pdf.savefig(fig)
pdf.close()

I found this was simple, highly customizable and OS-independent (as far as I know). I was able to implement this on a client's server without downloading any additional packages.

我发现这很简单,高度可定制且独立于操作系统(据我所知)。我能够在客户端的服务器上实现这一点,而无需下载任何额外的包。