使用PyPDF2合并2个以上的PDF

时间:2019-08-12 02:39:21

标签: python pdf merge pypdf2

我需要合并许多pdf,所以我编写了代码以合并它们。它没有用,我也不知道为什么。现在,该代码采用一个称为result的pdf,然后将其与另一个合并,然后将该结果称为result。它应该重复此操作,直到所有这些文件都合并为止,但是由于某种原因会出现问题。

from PyPDF2 import PdfFileMerger

pdfs = ['42.pdf', '43.pdf', '44.pdf', '45.pdf', '46.pdf', '47.pdf', '48.pdf', '49.pdf', '50.pdf', '51.pdf', '52.pdf', '53.pdf', '55.pdf', '56.pdf', '57.pdf', '58.pdf', '59.pdf', '60.pdf', '61.pdf', '62.pdf', '63.pdf', '64.pdf', '65.pdf', '66.pdf', '67.pdf', '68.pdf', '69.pdf', '70.pdf', '71.pdf', '72.pdf', '73.pdf', '74.pdf', '75.pdf', '76.pdf', '77.pdf', '78.pdf', '79.pdf', '80.pdf', '81.pdf', '82.pdf', '83.pdf', '84.pdf', '85.pdf', '86.pdf', '87.pdf', '88.pdf', '89.pdf', '90.pdf', '91.pdf', '92.pdf', '93.pdf', '94.pdf', '95.pdf', '96.pdf', '97.pdf', '98.pdf', '99.pdf', '100.pdf', '101.pdf', '102.pdf', '103.pdf', '104.pdf', '105.pdf', '106.pdf', '107.pdf', '108.pdf', '109.pdf', '110.pdf', '111.pdf', '112.pdf', '113.pdf', '114.pdf', '115.pdf', '116.pdf', '117.pdf', '118.pdf', '119.pdf', '120.pdf', '121.pdf', '122.pdf', '123.pdf', '124.pdf', '125.pdf', '126.pdf', '127.pdf', '128.pdf', '129.pdf', '130.pdf', '131.pdf', '132.pdf', '133.pdf', '134.pdf', '135.pdf', '136.pdf', '137.pdf', '138.pdf', '139.pdf', '140.pdf', '141.pdf', '142.pdf', '143.pdf', '144.pdf', '145.pdf', '146.pdf', '147.pdf', '148.pdf', '149.pdf', '150.pdf', '151.pdf', '152.pdf', '153.pdf', '154.pdf', '155.pdf', '156.pdf', '157.pdf', '158.pdf', '159.pdf', '160.pdf', '161.pdf', '162.pdf', '163.pdf', '164.pdf', '165.pdf', '166.pdf', '167.pdf', '168.pdf', '169.pdf']

def combine_pdfs(pdfs1):
    merger = PdfFileMerger()

    i = 0

    for pdf in pdfs1:
        merger.append(pdf)
        i += 1
        print(i)
    merger.write("result.pdf")
    merger.close()

r = 0
new_list = []
for file in pdfs:
    new_list.append("result.pdf")
    new_list.append(file)
    combine_pdfs(new_list)
    r = 0
    new_list = []

print("Done")

0 个答案:

没有答案