0

I am trying to write a python script which will read 2 pdf file and combine it to a new file but when i'm running the code below i'm not able to get desired please help

pdf1=open('1.pdf', 'rb')
pdf2=open('2.pdf', 'rb')
pdf3=open('new.pdf','ab')

chunk =200


while True:
    data=pdf1.read(chunk)
    if not data:
        break
    pdf3.write(data)


while True:
    data=pdf2.read(chunk)
    if not data:
        break
    pdf3.write(data)

pdf1.close()
pdf2.close()
pdf3.close()

  • PDFs may be too complicated to merge like that... try: https://stackoverflow.com/questions/3444645/merge-pdf-files – Larry the Llama Dec 01 '21 at 05:40
  • you can't join two PDF like this (the same with Excel files, images, JSON file, etc.) PDF has complex structure which can't be resize in this way. Every file may have ie. `header, data, footer` and you have to add `data` from one file in another file - and create new file with only one `header` and one `footer` – furas Dec 01 '21 at 05:47

0 Answers0