Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

append page to existing pdf file using python (and matplotlib?)

I would like to append pages to an existing pdf file.

Currently, I am using matplotlib pdfpages. however, once the file is closed, saving another figure into it overwrites the existing file rather than appending.

from matplotlib.backends.backend_pdf import PdfPages
import matplotlib.pyplot as plt



class plotClass(object):
    def __init__(self):
        self.PdfFile='c:/test.pdf'
        self.foo1()
        self.foo2()


    def foo1(self):
        plt.bar(1,1)
        pdf = PdfPages(self.PdfFile)
        pdf.savefig()
        pdf.close()

    def foo2(self):
        plt.bar(1,2)
        pdf = PdfPages(self.PdfFile)
        pdf.savefig()
        pdf.close()

test=plotClass()

I know appending is possible via multiple calls to pdf.savefig() before calling pdf.close() but I would like to append to pdf that has already been closed.

Alternatives to matplotlib would be appreciated also.

like image 283
jlarsch Avatar asked Jun 30 '16 09:06

jlarsch


People also ask

How do I add a page to a PDF in Python?

pdf"): print "Processing " + str(fname) + "...." # set up the pdfwriter and the input source output = PdfFileWriter() inputFile = "source/" + str(fname) input1 = PdfFileReader(file(inputFile, "rb")) # add the cover page to the PDF output output. addPage(inputcover.

How do you add text to a PDF in Python?

read your PDF using PdfFileReader() , we'll call this input. create a new pdf containing your text to add using ReportLab, save this as a string object. read the string object using PdfFileReader() , we'll call this text. create a new PDF object using PdfFileWriter() , we'll call this output.

What is Append to PDF?

The "Append" button will create a new document containing the existing PDF file and will add the new document information to the end of the file. The "Replace" button will overwrite the existing PDF file, and the "Cancel" button will cancel the PDF creation without affecting the existing PDF file.

Can you edit a PDF with Python?

Open a PDF in Python. Insert content at the beginning of the PDF document. Call the 'save()' method, passing the name of the output file with the required extension. Get the edited result.


2 Answers

You may want to use pyPdf for this.

# Merge two PDFs
from PyPDF2 import PdfFileReader, PdfFileWriter

output = PdfFileWriter()
pdfOne = PdfFileReader(open("path/to/pdf1.pdf", "rb"))
pdfTwo = PdfFileReader(open("path/to/pdf2.pdf", "rb"))

output.addPage(pdfOne.getPage(0))
output.addPage(pdfTwo.getPage(0))

outputStream = open(r"output.pdf", "wb")
output.write(outputStream)
outputStream.close()

example taken from here

Thereby you detach the plotting from the pdf-merging.

like image 181
ImportanceOfBeingErnest Avatar answered Sep 28 '22 00:09

ImportanceOfBeingErnest


I searched around for a while but could not find a way to append to the same pdf file after re-opening it elsewhere in the program. I ended up using dictionaries, that way I can store the figures to the dictionary for each pdf I'm interested in creating and write them to pdfs at the end. Here is an example:

dd = defaultdict(list)  #create a default dictionary
plot1 = df1.plot(kind='barh',stacked='True') #create a plot
dd[var].append(plot1.figure) #add figure to dictionary

#elsewhere in the program
plot2 = df2.plot(kind='barh',stacked='True') #another plot
dd[var].append(plot2.figure) #add figure to dictionary

#at the end print the figures to various reports
for var in dd.keys():
    pdf = PdfPages(var+.'pdf') #for each dictionary create a new pdf doc
    for figure in dd[k]:
        pdf.savefig(figure)   #write the figures for that dictionary
    pdf.close()
like image 23
sparrow Avatar answered Sep 28 '22 00:09

sparrow