Skip to content

Instantly share code, notes, and snippets.

@yui0
Last active April 20, 2022 02:25
Show Gist options
  • Save yui0/8f889dea14a33c8fa6737917b85597ad to your computer and use it in GitHub Desktop.
Save yui0/8f889dea14a33c8fa6737917b85597ad to your computer and use it in GitHub Desktop.
PDF for python
#! /usr/bin/python
# -*- coding: utf-8 -*-
# python3 make_report.py Relatorio.csv 3
# pip install pymupdf
import fitz
import os
import glob
import sys
import csv
csv_path = sys.argv[1]
month = int(sys.argv[2])
month = month -8
if month < 1: month = month +12
template_path = 'S-21_T.pdf'
def make_report(name, pub, video, hor, revisi, estudo, obs):
print('\n'+name)
print(hor)
#print(obs)
template = template_path
for fname in glob.glob("./**", recursive=True):
if not os.path.isfile(fname): continue
if not ".pdf" in fname: continue
if "2021" in fname: continue
if name in fname:
template = fname
name = os.path.splitext(fname)[0]
# 読み込み
print(template)
doc = fitz.open(template)
page = doc[0]
# 更新
for field in page.widgets():
#print(field.field_name)
#field.field_value = "new text"
if field.field_name == '901_'+ str(month) +'_S21_Value': field.field_value = str('{:.2f}'.format(pub))
elif field.field_name == '902_'+ str(month) +'_S21_Value': field.field_value = str('{:.2f}'.format(video))
elif field.field_name == '903_'+ str(month) +'_S21_Value': field.field_value = str('{:.2f}'.format(hor))
elif field.field_name == '904_'+ str(month) +'_S21_Value': field.field_value = str('{:.2f}'.format(revisi))
elif field.field_name == '905_'+ str(month) +'_S21_Value': field.field_value = str('{:.2f}'.format(estudo))
elif field.field_name == '906_'+ str(month) +'_S21_Value': field.field_value = obs
#elif field.field_name == '901_13_S21_Value': field.script_change = obs
else: continue
field.update()
# 書き込み
output_file = '/tmp/'+name+'.pdf'
if not os.path.isdir(os.path.dirname(output_file)): os.makedirs(os.path.dirname(output_file))
doc.save(output_file)
with open(csv_path) as f:
#print(f.read())
reader = csv.reader(f)
for row in reader:
if "Relatório" in row[0]: continue
if "Nome" == row[0]: continue
if "Total" == row[0]: break
#print(row)
#print(row[0]) # NAME
#print(row[4])
make_report(row[0], int(row[5] or 0), int(row[6] or 0), int(row[7] or 0), int(row[8] or 0), int(row[9] or 0), row[10])
#! /usr/bin/python
# -*- coding: utf-8 -*-
# pip install pypdf2
from PyPDF2.pdf import PdfFileReader, PdfFileWriter
import sys
template_path = sys.argv[1]
#template_path = 'template.pdf'
# 読み込み
reader = PdfFileReader(template_path)
existing_page = reader.getPage(0)
page_width = existing_page.mediaBox.getWidth()
page_height = existing_page.mediaBox.getHeight()
# formfieldsを取得
fields = reader.getFormTextFields()
# formの値を更新
for key in fields:
fields[key] = key
# writerを用意
writer = PdfFileWriter()
# 上書き
writer.updatePageFormFieldValues(existing_page, fields)
writer.addPage(existing_page)
# 書き込み
with open('output.pdf', 'wb') as output_file:
writer.write(output_file)
#! /usr/bin/python
# -*- coding: utf-8 -*-
# pip install pymupdf
import fitz
import sys
template_path = sys.argv[1]
doc = fitz.open(template_path)
page = doc[0]
for field in page.widgets():
print(field.field_name +": "+ str(field.script or ""))
#print(field.script) # for button
print(field.script_stroke)
print(field.script_format)
print(field.script_change)
print(field.script_calc)
print(field.field_type)
print(field.field_flags)
#for annot in page.annots():
#print(annot)
#annot = page.firstAnnot
#while annot:
#if annot.type[0] != 19:
#annot = annot.next
#continue
#print(annot.widget_type, annot.info["title"].ljust(25), annot.widget_text())
#annot = annot.next
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment