python - 使用 Python 将 Pandas DataFrame 导出为 PDF 文件

标签 python pandas pdf reportlab

在 Pandas 中为数据框生成 PDF 的有效方法是什么?

最佳答案

首先使用 matplotlib 绘制表格,然后生成 pdf

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages

df = pd.DataFrame(np.random.random((10,3)), columns = ("col 1", "col 2", "col 3"))

#https://stackoverflow.com/questions/32137396/how-do-i-plot-only-a-table-in-matplotlib
fig, ax =plt.subplots(figsize=(12,4))
ax.axis('tight')
ax.axis('off')
the_table = ax.table(cellText=df.values,colLabels=df.columns,loc='center')

#https://stackoverflow.com/questions/4042192/reduce-left-and-right-margins-in-matplotlib-plot
pp = PdfPages("foo.pdf")
pp.savefig(fig, bbox_inches='tight')
pp.close()

引用:

How do I plot only a table in Matplotlib?

Reduce left and right margins in matplotlib plot

关于python - 使用 Python 将 Pandas DataFrame 导出为 PDF 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33155776/

相关文章:

swift - 如何给 PDF 数据一个文件名供用户在 Swift 中保存

c# - AmyuniPDF 以错误的字体(特殊字符)打印 PDF 文档

python - 通过在下拉列表中导航不同的选项来抓取表格

python - 删除在pyspark中使用numpy.savetxt创建的csv文件

python - 验证 JSONLint 上的 AWS json 响应

python - read_table pandas python 数字错误

python - 确保千克转换为克 pandas

python - 我可以按列分组并重新采样日期吗?

ios - PoDoFo 库在 ios 7 中崩溃但在 ios 6 及更早版本中工作

python - 如何按\n 拆分嵌套列表但跳过第一个? Python