Add page number to PDF

what is this

Here's how to ** edit a PDF for free ** using Python **. As a sample, let's add a page number to each page.

image

Installation

Use the following:

-ReportLab: PDF generation library for Python. There is a paid version and a free version. -PyPDF2: Page-based PDF editing library. -PdfFormFiller: A library for inserting text into PDF.

Installation


conda install -y reportlab
pip install PyPDF2 pdfformfiller

If you are not using Anaconda, use pip instead of conda.

Sample to add page number

IPAex Gothic font is used as the Japanese font, but another font may be used. (On Ubuntu, you can install it with apt-get install fonts-ipaexfont)

Note that FloatObject, which is an element of PyPDF2.pdf.PageObject.mediaBox that represents the paper size, returns Decimal, so an error will occur in the addition / subtraction operation with the real number. Here, I forcibly replace FloatObject so that it can be added or subtracted from a real number.

With addPage (input file, output file), you can create a PDF with page numbers added to the original PDF.

python


import PyPDF2
class FloatObject(PyPDF2.generic.FloatObject):
    def __add__(self, other):
        return self.as_numeric() + other
    def __radd__(self, other):
        return self.as_numeric() + other
    def __sub__(self, other):
        return self.as_numeric() - other
    def __rsub__(self, other):
        return -self.as_numeric() + other
PyPDF2.generic.FloatObject = FloatObject

from reportlab.lib.styles import ParagraphStyle
from reportlab.lib.enums import TA_CENTER
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from pdfformfiller import PdfFormFiller

def addPage(infile, outfile):
    #On Linux'/usr/share/fonts/opentype/ipaexfont-gothic/ipaexg.ttf'Such
    pdfmetrics.registerFont(TTFont('IPAexGothic', 'c:/Windows/Fonts/ipaexg.ttf'))
    sty = ParagraphStyle('sty', alignment=TA_CENTER, fontName='IPAexGothic', fontSize=9)
    ff = PdfFormFiller(infile)
    for i in range(ff.pdf.getNumPages()):
        p = ff.pdf.getPage(i)
        ff.add_text('page%d'%(i+1), i, (0,p.mediaBox[3]-30), p.mediaBox.getUpperRight(), sty)
    ff.write(outfile)

--Windows, Ubuntu, Alpine-We have confirmed the operation on Linux.

Postscript

I packaged it.

https://pypi.org/project/addpage/

that's all

Recommended Posts

Add page number to PDF
How to add page numbers to PDF files (in Python)
[Wagtail] Add a login page to the Wagtail project
[Python] Continued-Convert PDF text to CSV page by page
Add convolution to MNIST
[Python] Convert PDF text to CSV page by page (2/24 postscript)
Add a dictionary to MeCab
Add System to pyenv-win versions
[Memo to add] Page to see when developing with GAE / P
Add user dictionary to MeCab
Convert a large number of PDF files to text files using pdfminer
Function to convert Excel column to number
Add fields to features with ArcPy
Convert markdown to PDF in Python
Convert A4 PDF to A3 every 2 pages
Add cumulative ratio to matplotlib histogram
Conversion from pdf to txt 1 [pdfminer]
Add Python 2.7 Japanese documentation to Dash.app
How to add sudo when debugging
Convert from pdf to txt 2 [pyocr]
Add parameters to Django's custom commands
How to add AWS EBS volume
Convert PDF to image with ImageMagick
Add TRACE log level to Python ...?
Add SSH connectable users to EC2
Get the number of visits to each page with ReportingAPI + Cloud Functions