Combining two PDF files

import PyPDF2 import glob import os from fpdf import FPDF import shutil class MyPDF(FPDF): # adding a footer, containing the page number def footer (self): self.set_y(-15) self.set_font("Arial", Style="I", size=8) pageNum = "page %s/{nb}" % self.page_no() self.cell(0,10, pageNum, align="C") if __name__ == "__main__": os.chdir("pathtolocation/docs/") # docs location os.system("libreoffice --headless --invisible --convert-to pdf *") # this converts everything to pdf for file in glob.glob("*"): if file not in glob.glob("*.pdf"): shutil.move(file,"/newlocation") # moving files we don't need to another folder # adding the cover and footer path = open(file, 'wb') path2 = open ('/pathtocover/cover.pdf') merger = PyPDF2.PdfFileMerger() pdf = MyPDF() for file in glob.glob("*.pdf"): pdf.footer() merger.merge(position=0, fileobj=path2) merger.merge(position=0, fileobj=path) merger.write(open(file, 'wb')) 

This script is converted to pdf, adds the cover to pdf and the footer containing the page number captures some things, and now I run it for the last time to see if it works, takes too much time, there are no errors, I did something- is it wrong or does he need to merge for a long time and add footers? I work with 3 files, and he quickly converted them.

Exception Exit

 convert /home/projects/convert-pdf/docs/sample (1).doc -> /home/projects/convert-pdf/docs/sample (1).pdf using writer_pdf_Export 

so it transforms and moves, I think the problem is here somewhere

  for file in glob.glob("*.pdf"): pdf.footer() merger.merge(position=0, fileobj=path2) merger.merge(position=0, fileobj=path) merger.write(open(file, 'wb')) 

Since I'm trying to combine position=0 with position=0 , I'm not sure about that, though

+4
python pdf pdf-generation pypdf
source share
1 answer

This is actually better than a comment, but I want to show the code. You need to add some try blocks to catch any errors - this is something super basic that you do.

 import PyPDF2 import glob import os from fpdf import FPDF import shutil class MyPDF(FPDF): # adding a footer, containing the page number def footer (self): try: self.set_y(-15) self.set_font("Arial", Style="I", size=8) pageNum = "page %s/{nb}" % self.page_no() self.cell(0,10, pageNum, align="C") except Exception, err: print "Error applying footer: {}".format(err) if __name__ == "__main__": try: os.chdir("pathtolocation/docs/") # docs location os.system("libreoffice --headless --invisible --convert-to pdf *") # this converts everything to pdf for file in glob.glob("*"): if file not in glob.glob("*.pdf"): shutil.move(file,"/newlocation") # moving files we don't need to another folder # adding the cover and footer path = open(file, 'wb') path2 = open ('/pathtocover/cover.pdf') merger = PyPDF2.PdfFileMerger() pdf = MyPDF() except Exception, err: print "error setting up the pdf: {}".format(err) for file in glob.glob("*.pdf"): try: pdf.footer() merger.merge(position=0, fileobj=path2) merger.merge(position=0, fileobj=path) merger.write(open(file, 'wb')) except Exception, err: print "Error processing glob: {}".format(err) 
+2
source share

All Articles