Here's how to ** edit a PDF for free ** using Python **. As a sample, let's add a page number to each page.
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.
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.
I packaged it.
https://pypi.org/project/addpage/
that's all
Recommended Posts