8

I'm using PdfPages from matplotlib and I can loop through each figure object and save each one as a separate page in the same PDF:

from matplotlib.backends.backend_pdf import PdfPages
pp = PdfPages('output.pdf')
for fig in figs:
    pp.savefig(fig)
pp.close()

This works great. But is there a way for me to add a page number for each page in the PDF?

Thanks.

mortada
  • 1,658
  • 2
  • 14
  • 12
  • I don't know of any direct way to do it, here's a brief outline of another possible solution. Save the plot as an image, insert the image into a word document, add pages to the word doc, then save it as a pdf. Its a little roundabout, but if you think this might be a good way to go let me know and I can flush it out into an actual answer – wnnmaw Aug 06 '14 at 15:56
  • I need to do this once a day with a lot of images, so I'd really need an automated way – mortada Aug 06 '14 at 20:27
  • 2
    It could be automated, but maybe look into [this question](http://stackoverflow.com/questions/1180115/add-text-to-existing-pdf-using-python) to work with the pdf directly – wnnmaw Aug 06 '14 at 20:40

7 Answers7

8

A nice solution using reportlib and PyPDF (base on this):

import os

from PyPDF4.pdf import PdfFileReader, PdfFileWriter
from reportlab.lib.units import mm
from reportlab.pdfgen import canvas


def create_page_pdf(num, tmp):
    c = canvas.Canvas(tmp)
    for i in range(1, num + 1):
        c.drawString((210 // 2) * mm, (4) * mm, str(i))
        c.showPage()
    c.save()


def add_page_numgers(pdf_path):
    """
    Add page numbers to a pdf, save the result as a new pdf
    @param pdf_path: path to pdf
    """
    tmp = "__tmp.pdf"

    writer = PdfFileWriter()
    with open(pdf_path, "rb") as f:
        reader = PdfFileReader(f, strict=False)
        n = reader.getNumPages()

        # create new PDF with page numbers
        create_page_pdf(n, tmp)

        with open(tmp, "rb") as ftmp:
            number_pdf = PdfFileReader(ftmp)
            # iterarte pages
            for p in range(n):
                page = reader.getPage(p)
                numberLayer = number_pdf.getPage(p)
                # merge number page with actual page
                page.mergePage(numberLayer)
                writer.addPage(page)

            # write result
            if writer.getNumPages():
                newpath = pdf_path[:-4] + "_numbered.pdf"
                with open(newpath, "wb") as f:
                    writer.write(f)
        os.remove(tmp)
Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
ofir dubi
  • 533
  • 2
  • 6
  • 14
6

Something like this:

from matplotlib.backends.backend_pdf import PdfPages
pp = PdfPages('output.pdf')
for n, fig in enumerate(figs):
    fig.text(4.25/8.5, 0.5/11., str(n+1), ha='center', fontsize=8)
    pp.savefig(fig)
pp.close()
Steve Schulist
  • 931
  • 1
  • 11
  • 18
2

Use numbering2pdf library.

from numbering2pdf import add_numbering_to_pdf

add_numbering_to_pdf("old_file.pdf", "new_file.pdf")
1

Either PyPDF2 or pdfrw will let you overlay two PDFs (so, for example, you can generate a PDF which is only page numbers, and use it to watermark your images). pdfrw has a watermark example that uses a single watermark page, but this could easily be modified to use a set of watermark pages, one for each page number.

If you want to get fancier, you can use reportlab to generate these pages on the fly.

pdfrw also has a facility that allows you to import a PDF page into reportlab as if it were an image. There are a couple of examples around that do this dynamically -- here is a good starting point.

Finally, rst2pdf (which is not all that well maintained but works well for simple cases) also lets you import PDFs as images -- it uses pdfrw and reportlab under the hood -- so you can easily use restructuredText to create documents with your images embedded. AFAIK, the best reportlab to use with the released version of rst2pdf is 2.7.

(Disclaimer: I am the pdfrw author and have made contributions to rst2pdf.)

Community
  • 1
  • 1
Patrick Maupin
  • 8,024
  • 2
  • 23
  • 42
1

PyPDF2 >= 2.10.0

Requirements:

# generate a page with a page number:
pip install reportlab --upgrade

# merge that numbered (otherwise empty) page with the original:
pip install PyPDF2 --upgrade

Using a slightly modified version of the code of ofir dubi:

import os

from PyPDF2 import PdfReader, PdfWriter
from reportlab.lib.units import mm
from reportlab.pdfgen import canvas


def create_page_pdf(num, tmp):
    c = canvas.Canvas(tmp)
    for i in range(1, num + 1):
        c.drawString((210 // 2) * mm, (4) * mm, str(i))
        c.showPage()
    c.save()


def add_page_numgers(pdf_path, newpath):
    """
    Add page numbers to a pdf, save the result as a new pdf
    @param pdf_path: path to pdf
    """
    tmp = "__tmp.pdf"

    writer = PdfWriter()
    with open(pdf_path, "rb") as f:
        reader = PdfReader(f)
        n = len(reader.pages)

        # create new PDF with page numbers
        create_page_pdf(n, tmp)

        with open(tmp, "rb") as ftmp:
            number_pdf = PdfReader(ftmp)
            # iterarte pages
            for p in range(n):
                page = reader.pages[p]
                number_layer = number_pdf.pages[p]
                # merge number page with actual page
                page.merge_page(number_layer)
                writer.add_page(page)

            # write result
            if len(writer.pages) > 0:
                with open(newpath, "wb") as f:
                    writer.write(f)
        os.remove(tmp)


if __name__ == "__main__"
    add_page_numgers("input.pdf", "output.pdf")
Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
0

Here is my answer which uses Matplotlib PDF backend to generate a PDF w/ just page numbers and PyPDF2 to merge the "footer" PDF w/ the desired PDF:

def add_header_footer(source_path, save_path, footer_pdf_path=None, start_page=0, header_text=None):
    ''' Adds header & footer info to existing PDFs '''
    footer_pdf_path = os.path.join(os.path.dirname(source_path), 'footer.pdf')
    reader = PdfReader(source_path)
    writer = PdfWriter()
    n_pages = len(reader.pages)

    # Step 1: generate header/footer PDF to be merged into source PDF
    pp = PdfPages(footer_pdf_path)
    for p in range(n_pages):
        fig = plt.figure(num=613, figsize=(8.5, 11), constrained_layout=1, facecolor='white')
        fig.patch.set_alpha(0)
        fig.text(0.48, 0.04, f'{p + 1} | <FOOTER TEXT>',
                 horizontalalignment='center', weight='bold',
                 verticalalignment='bottom', fontsize=6, color='grey')
        if header_text is not None:
            fig.text(0.1, 0.95, header_text,
                     horizontalalignment='left', weight='bold',
                     verticalalignment='center', fontsize=6, color='grey')
        pp.savefig(fig)
        plt.close()
    pp.close()
    footer_reader = PdfReader(footer_pdf_path)

    # Step 2: merge source PDF & header/footer PDF
    for index in list(range(start_page, n_pages)):
        content_page = reader.pages[index]
        footer_page = footer_reader.pages[index]
        mediabox = content_page.mediabox
        content_page.merge_page(footer_page)
        content_page.mediabox = mediabox
        writer.add_page(content_page)

    # Step 3: save merged PDF
    with open(save_path, "wb") as fp:
        writer.write(fp)

    return None
DankMasterDan
  • 1,900
  • 4
  • 23
  • 35
0

You can also use fpdf2 (pip install fpdf2). If you have the images saved then you can do something like this:

from fpdf import FPDF
import glob

class MyPDF(FPDF):
    def footer(self):
        # position footer from bottom of page
        self.set_y(-0.6)
        # set the font, I=italic
        self.set_font("helvetica", style="I", size=8)
        # set page number and center it
        pageNum = f'- {self.page_no()} -'
        self.cell(0, 0.5, pageNum, align="C")

filenames = glob.iglob('*.jpg')
pdf = MyPDF()
pdf = MyPDF(orientation='P', unit='in', format='Letter')
for fname in filenames:
    pdf.add_page(orientation='P')
    pdf.image(fname, x=1.0, h=4.8)
pdf.output('Images.pdf')
screenpaver
  • 1,120
  • 8
  • 14