0

I need to merge many pdfs, so I made a code to merge them. It wasn't working, and I have no idea why. Right now the code takes one pdf called result, and merges it with another, and then the result of that one is called result. It should repeat this until all of them are merged, but for some reason there is a problem.

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")
Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
Jack N
  • 324
  • 2
  • 14
  • did you look at [this](https://stackoverflow.com/questions/3444645/merge-pdf-files/37945454#37945454) answer? – Paul Rooney Aug 12 '19 at 02:47
  • Ya. I did. It wasn't working for me to merge more than 2 pdfs. They are quite large, which may be an issue. I will need to do a bit more experimenting to determine the problem and find a solution. – Jack N Aug 13 '19 at 05:57
  • oh really I'd be interested to know in what way it wasn't working. Is there some exception thrown or did it produce incorrect results? Perhaps there is a tipping point as far as file size goes where too much memory is used. – Paul Rooney Aug 13 '19 at 05:59

0 Answers0