首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不使用pdfkit将熊猫DataFrame保存为PDF文件格式

不使用pdfkit将熊猫DataFrame保存为PDF文件格式
EN

Stack Overflow用户
提问于 2018-08-23 03:36:17
回答 2查看 18.7K关注 0票数 4

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

代码语言:javascript
复制
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个包和方法,但都不能解决它。

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

提前谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-08-23 03:46:09

一种选择是从以下开始:

代码语言:javascript
复制
df.to_html()

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

代码语言:javascript
复制
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上进行了测试,结果是肯定的。

票数 9
EN

Stack Overflow用户

发布于 2019-06-13 07:43:44

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

代码语言:javascript
复制
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()

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

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51973991

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档