python - 将 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 个包和方法,但无法解决。
还有更多我可以尝试的软件包吗?这让我得了癌症
提前致谢。
解决方案
一种选择是从以下开始:
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 上进行了测试,结果是肯定的。
推荐阅读
- node.js - E/launcher - 未创建会话:此版本的 Chrome 驱动程序在运行量角器测试时仅支持 Chrome 版本 93
- r - 如何从标签切割间隔中删除 E 符号并用速记标签替换?
- javascript - Sweet Alert 2 图标无法正确呈现
- arrays - 为什么我的数组中出现负值?
- merge - 在合并查询中使用不匹配时配置单元中的多个案例语句
- javascript - Javascript使用js向元素添加类
- css - 嵌套的 flexbox 溢出屏幕
- java - 这两个标签在 Pitest XML 报告中代表什么?
- elasticsearch - Kibana 中使用动态 Signal 作为 Elasticsearch 查询参数的 Vega 可视化
- oauth-2.0 - 如何从非 Azure/MS 服务中提取 OAuth 2 令牌