Hometown tax payment One-stop special system donation tax credit filing special application form in Python

Hometown tax payment One-stop special system donation tax credit filing special application form in Python

The application form must arrive at 1/10 when using the hometown tax payment one-stop special system. Since it was difficult to fill in by hand, I created a script to fill in the required items in the distributed pdf.

It runs on google Colaboratory.

Library preparation

#Library required for pdf editing
!pip install PyPDF2 reportlab
#Font for writing Japanese to pdf
!apt-get -y -q install fonts-ipaexfont

import io
from PyPDF2 import PdfFileWriter
from reportlab.lib.colors import Color
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.cidfonts import UnicodeCIDFont
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.pdfgen import canvas
from reportlab.lib.units import mm
from reportlab.lib.pagesizes import A4, portrait

#Register font
pdfmetrics.registerFont(
    TTFont(
        'IPAexPMincho',
        '/usr/share/fonts/opentype/ipaexfont-mincho/ipaexm.ttf'
    )
)

#Information to write
address = ["〇〇 Town, 〇〇 Ward, Tokyo", "123-123", "Corp 303"]
tel = "03-0000-0000"
hurigana = "Yamada Taro"
name = "Yamada Taro"
number = "000000000000"
sex = "male"
birth = ("Ordinance", "1", "1" ,"1")
wareki = "2"

data = [
    ("〇〇 city", 2, 12, 1,100000),
    ("△△ town", 2, 12, 1,50000),
]

Processing to write characters

def write_common(can):
    font_size = 10

    left = 49*mm
    right = 113*mm
    can.setFont('IPAexPMincho', font_size)
    can.setFillColor(Color(0, 0, 0, alpha=1))

    #Street address
    for i, row in enumerate(address):
        can.drawString(left, (37*mm + (font_size + 5) * i), row)

    #phone number
    can.drawString(left, 59*mm, tel)

    #Frigana
    can.drawString(right, 33*mm, hurigana)

    #Full name
    can.drawString(right, 39.5*mm, name)

    #personal number
    for i, num in enumerate(number):
        can.drawString(right - 2.1*mm + (i * 4.1)*mm, 47*mm, num)

    #sex
    if sex == "male":
        maru = right + 13*mm
    else:
        maru = right + 27.4*mm
    can.drawString(maru, 52.7*mm, "○")

    #Birthday
    if birth[0] == "Ming":
        nengou = (right - 1.5*mm, 58*mm)
    elif birth[0] == "Big":
        nengou = (right + 2.7*mm, 58*mm)
    elif birth[0] == "Akira":
        nengou = (right + 6.6*mm, 58*mm)
    elif birth[0] == "flat":
        nengou = (right + 1.5*mm, 61*mm)
    elif birth[0] == "Ordinance":
        nengou = (right + 2.7*mm, 61*mm)
    can.drawString(*nengou, "○")
    birth_height = 60*mm
    can.drawString(right + 16*mm, birth_height, birth[1])
    can.drawString(right + 27*mm, birth_height, birth[2])
    can.drawString(right + 37*mm, birth_height, birth[3])

    #Japanese Calendar
    can.drawString(45*mm, 14*mm, wareki)

    #check
    can.drawString(151*mm, 161*mm, "✓")
    can.drawString(151*mm, 200*mm, "✓")

    #date entered
    can.setFont('IPAexPMincho', 8)
    date_height = 26*mm
    can.drawString(40*mm, date_height, "2")
    can.drawString(51*mm, date_height, "12")
    can.drawString(63*mm, date_height, "31")
def write_kifu(can, kifu_data):
    can.setFont('IPAexPMincho', 10)
    can.drawRightString(68*mm, 29*mm, kifu_data[0] + "Long")
    kifu_height = 134.5*mm
    can.drawString(51*mm, kifu_height, str(kifu_data[1]))
    can.drawString(62*mm, kifu_height, str(kifu_data[2]))
    can.drawString(74*mm, kifu_height, str(kifu_data[3]))
    can.drawRightString(139*mm, kifu_height, f"{kifu_data[4]:,}")

Write to file

from PyPDF2 import PdfFileReader
#Read the pdf of the distributed application form
pdf_path = "./onestop_myNumber_form.pdf"

width, height = portrait(A4)
output = PdfFileWriter()

for row in data:
    packet = io.BytesIO()
    can = canvas.Canvas(packet, pagesize=(width, height), bottomup=False)
    write_common(can)
    write_kifu(can, row)
    can.showPage()
    can.save()

    packet.seek(0)
    new_pdf = PdfFileReader(packet)

    pdf = PdfFileReader(pdf_path)
    page = pdf.getPage(0)

    page.mergePage(new_pdf.getPage(0))
    output.addPage(page)


with open(f"./kifu.pdf", "wb") as fout:
    output.write(fout)

Complete!

kifu_〇〇市.png

Helpful articles

https://buildersbox.corp-sansan.com/entry/2020/06/09/110000 https://news.mynavi.jp/article/zeropython-70/

Recommended Posts

Hometown tax payment One-stop special system donation tax credit filing special application form in Python
Submit in [Python] form