Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merging two PDFs

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 converts to pdf, add a cover to the pdf and footer containing the page number, fixed some stuff and now I run it for the last time to see if it's working, it's taking too much time, no error, did I do something wrong or does it need that long to merge and add footers? I'm working with 3 files, and it converted them so fast.

Exception output

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

so it is converting and moving, I think the problem is somewhere here

   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 merge position=0 with position=0, not sure about it though

like image 694
Lynob Avatar asked Sep 29 '22 22:09

Lynob


1 Answers

This is actually better as a comment, but I want to show code. You need to add some try blocks in there to catch any errors - here is something super basic you coudld 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)
like image 139
picus Avatar answered Oct 03 '22 01:10

picus