繁体   English   中英

Python:根据名称合并pdf

[英]Python: Combine pdfs based on name

我编写了一些代码,可为数据框中的每一列创建不同图形的pdf。 将pdf保存在与保存代码相同的文件夹中。我使用列名和图形类型描述的组合保存了pdf。 我提供了一种类型的图的样本。 该图另存为“ columnname_histogram.pdf”

############################# HISTOGRAM ###############################################

palette = sns.color_palette(palette=sns.crayon_palette(sns.colors.crayons))
new_palette = itertools.cycle(palette)         
for i in data:  # Loop over all columns 
    k =data[i].astype(float) #Changing to float
    sns.set() #defaults the background
    fig, ax = plt.subplots()
    sns.set(style="ticks") #darkens grid lines
    sns.distplot(k,color=next(new_palette))  #sets which column to use
    sns.despine(offset=10, trim=True) 
    fig.set_size_inches(18,12)
    ax.set_title('{} Histogram'.format(i), fontweight='bold') #sets chart title based on column
    plt.savefig('{}_hist.pdf'.format(i), bbox_inches='tight')  #sets file name based on column name

除了直方图,我还具有相同命名约定的小提琴图和历史线图。

我的问题是,如何获取这些单独的pdf文件并将它们放入一个pdf文件以便于查看? 我正在尝试将具有相同列名的所有pdf合并到一个文档中。 我可以遵循的任何建议或示例代码吗? 我没有运气尝试过自己。

谢谢!

如果您有定义的模式可用于标识要合并的pdf,则可以使用PyPDF2将文件合并在一起:

from PyPDF2 import PdfFileMerger
import os    

colnames = ["col1", "col2"] # list of column names
for colname in colnames:    
    filemerger = PdfFileMerger()
    for file in os.listdir("/yourpdfdir"):
        if file.endswith(".pdf") and file.startswith(colname):
            filemerger.append(file)
    filemerger.write(colname+"_combined.pdf")
    filemerger.close()

暂无
暂无

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

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