简体   繁体   中英

Merge multiple PDfs using pypdf2 and Email

I am using PyPDF2 to merge multiple pdf files and then use python shutil method to copy the output to the final destination and use smtplib to email a copy(as an attachment) to a user.

The merged files in the final destination is appearing correctly but the emailed copy(attachment) is not. Only fist item in an array is appearing but rest page is blank with correct total number of pages.

Here is my workflow

  • Merge Files and save merged to local folder --- Working as expected
  • Copy merged file to final destination --- Working as expected -
  • Email a copy of merged file to user email address -- First file in an array, empty pages after that. The total number of pages matches with the total count.

I am using following code to merge files

pdf_list=['pdf_source.pdf','second_file.pdf','third_file.pdf']
for pdf in pdf_list:            
            merger.append(PdfFileReader(pdf),'rb')  
with open(merge_file_name, 'wb') as new_file:
            merger.write(new_file)

Please suggest

After researching, I found the issue was not pypdf2 but the way file was being attached to an email.

Following stackoverflow solution fixed the issue.

How to attach a pdf file to a MIME email in Python?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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