【问题标题】:Combine PDF's with specific names from two different folders using PyPDF2使用 PyPDF2 将 PDF 与来自两个不同文件夹的特定名称组合
【发布时间】:2021-08-03 13:02:21
【问题描述】:

我有两个文件夹,其中包含一组不同的 pdf。我知道第一个文件夹中具有特定名称的 PDF 需要与第二个文件夹中具有特定名称的 PDF 组合。例如,第一个文件夹中的“PID-01.pdf”需要与第二个文件夹中的“FNN-PID-01.pdf”组合,第一个文件夹中的“PID-02.pdf”需要与“ FNN-PID-02.pdf”来自第二个文件夹,我有两个文件夹,依此类推。我正在使用 python 模块 PyPDF2。谁能举一个使用 PyPDF2 的例子

【问题讨论】:

    标签: python python-3.x pypdf2


    【解决方案1】:

    您是说“合并”是指“合并”吗?

    如果是这样,

    假设 folder1 包含 "PID-01.pdf",而 folder2 包含 "FNN-PID-01.pdf"

    import os
    from PyPDF2 import PdfFileMerger, PdfFileReader
    folder1 = "/your/path/to/folder1/"
    folder2 = "/your/path/to/folder2/"
    merged_folder = "/your/path/to/merged/folder/"
    
    f1_files = os.listdir(folder1) # ['PID-01.pdf','PID-02.pdf'...etc]
    f2_files = os.listdir(folder2) # ['FNN-PID-01.pdf','FNN-PID-02.pdf'...etc]
    
    def pdf_merger(f1,f2):
        merger = PdfFileMerger()
        f1_content = PdfFileReader(file(os.path.join(folder1,f1), 'rb'))
        f2_content = PdfFileReader(file(os.path.join(folder2,f2), 'rb'))
        merger.append(f1_content)
        merger.append(f2_content)
        out = os.path.join(merged_folder,f"merged-{f1}")
        merger.write(out)
    
    #below code will iterate each file in folder1 and checks if those               
    #folder2 filename string "FNN-PID-01.pdf" contains substring "PID-01.pdf"
    #if matchs, the 2 matching files are merged and saved to merged_folder
    
    for file1 in f1_files : 
        for file2 in f2_files: 
            if file1 in file2: 
                pdf_merger(file1,file2)
    

    您可以使用正则表达式迭代文件并编写自己的匹配模式以进行高级使用。

    【讨论】:

    • 非常感谢您帮助我。我对您的代码进行了一些更改,如下所示: f1_content = PdfFileReader(open(os.path.join(folder1,f1),'rb')) f2_content = PdfFileReader(open(os.path.join(folder2,f2),' rb')) for file1 in f1_files: for file2 in f2_files: if str(f"FNN-{file1}") == file2: pdf_merger(file1,file2)
    • 你不必声明 str(f"xyz") 因为 f" 本身就是一个字符串。
    【解决方案2】:

    这是一个教学示例:

    from PyPDF4 import PdfFileReader, PdfFileWriter
    #from PyPDF2 import PdfFileReader, PdfFileWriter
    
    
    def concatenate(pdf_out, *pdfs):
        # initialize a write instance
        pdf_w = PdfFileWriter()
    
        for pdf in pdfs:
            pdf_r = PdfFileReader(open(pdf, 'rb')) # pass a binary descriptor to the pdf reader
            pdf_w.appendPagesFromReader(pdf_r)
    
        with open(pdf_out, 'w') as fd:
            pdf_w.write(fd)     # write binary stream of data to destination
    
    
    pdf1 = 'dir1/PID-01.pdf'
    pdf2 = 'dir2/PID-01.pdf'
    pdf_out = '?/?.pdf' # choose where to save the merged file
    
    concatenate(pdf_out, pdf1, pdf2)
    
    import os
    # under the assumption the both folder have the same amount files
    dir_1 = #
    dir_2 = #
    dir_target = #
    counter = 1
    for pdf1, pdf2 in zip(os.listdir(dir1), os.listdir(dir2)):
        pdf_new_path = os.path.join(dir_target, 'PID-PNN-{}.pdf'.format(counter)) # or choose another filename pattern
    
        concatenate(pdf_new_path, pdf1, pdf2)
        counter += 1
    
    

    备注

    PyPDF2PyPDF4 几乎(?)向后兼容,所以只需更改导入

    函数是顺序敏感的! pdf1 排名第一,然后pdf2 在最终文件中

    【讨论】:

      猜你喜欢
      • 2017-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-22
      • 1970-01-01
      • 1970-01-01
      • 2019-04-03
      相关资源
      最近更新 更多