我一直在尝试使用pdfpages在pdf文件中添加dataframe。但我还没有找到任何opt解决方案。我的数据帧大约有5k行和10列。如何将其附加到pdf?
我写的代码给出了一个非常模糊和小的df。就像非常非常小,即使你放大了,你也只能看到模糊的东西。有没有使用pdfpages将df添加到pdf的最佳方法?
我的代码:
df1 = data[data['olddate'].dt.date == data['newdate'].dt.date]
table = pd.DataFrame(df1)
fig = plt.figure(figsize=(12, 12))
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')
export_pdf.savefig(fig)
plt.close()发布于 2019-09-05 19:39:03
这里有一个例子,希望能有所帮助:
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
import numpy as np
with PdfPages('page_pdf.pdf') as pdf:
table = pd.DataFrame({'a':[1,2,3,4], 'b':[3,4,5,6]})
header = table.columns
table = np.asarray(table)
fig = plt.figure(figsize=(12, 12))
ax = plt.Axes(fig, [0., 0., 1., 1.])
ax.set_axis_off()
fig.add_axes(ax)
tab = plt.table(cellText=table, colWidths=[0.15, 0.25], colLabels=header, cellLoc='center', loc='center')
tab.auto_set_font_size(False)
tab.set_fontsize(30)
tab.scale(0.7, 2.5)
pdf.savefig(fig)
plt.close()您可以使用tab.set_fontsize更改大小
https://stackoverflow.com/questions/57800113
复制相似问题