pdf_overwrite.py
#! /usr/bin/python
#
# pdf_overwrite.py
#
# Oct/02/2018
#
# ------------------------------------------------------------------
import sys
from pdfrw import PdfReader
from pdfrw.buildxobj import pagexobj
from pdfrw.toreportlab import makerl
from reportlab.pdfgen import canvas
from reportlab.pdfbase.cidfonts import UnicodeCIDFont
from reportlab.pdfbase import pdfmetrics
# ------------------------------------------------------------------
sys.stderr.write ("*** 開始 ***\n")
file_in = sys.argv[1]
file_out = sys.argv[2]
sys.stderr.write(file_in + "\n")
sys.stderr.write(file_out + "\n")
#
cc = canvas.Canvas(file_out)
fontname_g = "HeiseiKakuGo-W5"
pdfmetrics.registerFont (UnicodeCIDFont (fontname_g))
cc.setFont(fontname_g,16)
page = PdfReader(file_in,decompress=False).pages
pp = pagexobj(page[0])
cc.doForm(makerl(cc, pp))
cc.drawString(100,600,"こんにちは")
cc.drawString(150,500,"本日は晴天なり。")
cc.drawString(100,400,"Hello World")
cc.showPage()
cc.save()
#
sys.stderr.write ("*** 終了 ***\n")
# ------------------------------------------------------------------
実行方法
./pdf_overwrite.py template.pdf out01.pdf