简体   繁体   English

如何使用pdfpages将数据框添加到pdf文件

[英]How to add dataframe to pdf file using pdfpages

I have been trying to find a way to add dataframe in a pdf file using pdfpages.我一直在尝试找到一种使用 pdfpages 在 pdf 文件中添加数据框的方法。 But I did not find any opt solution yet.但我还没有找到任何 opt 解决方案。 My dataframe has around 5k rows and 10 columns.我的数据框有大约 5k 行和 10 列。 How do i append it to pdf?我如何将其附加到pdf?

The code I have written gives a very blurry and small df.我写的代码给出了一个非常模糊和小的 df。 Like very very small and even if you zoom in, you can only see blurry stuff.就像非常非常小,即使放大,也只能看到模糊的东西。 Any optimal way to add df to pdf using pdfpages?使用pdfpages将df添加到pdf的任何最佳方法?

my code:我的代码:

  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()

Here there is an example, hope it can help:这里有一个例子,希望它可以帮助:

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()

You can change the size with tab.set_fontsize您可以使用tab.set_fontsize更改大小

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM