forked from kmsdc/edulc
89 lines
3.7 KiB
Python
89 lines
3.7 KiB
Python
from configparser import ConfigParser
|
||
#import sys
|
||
from PIL import Image, ImageDraw, ImageFont
|
||
from serial import get_serial
|
||
|
||
|
||
|
||
# конфигураци файл этого модуля. должен находиться в каталоге исполняемой программы, простите.
|
||
CONFIG_FILE_NAME = 'lpe.ini'
|
||
global Template_dir
|
||
global FontsDir
|
||
global Fonts
|
||
|
||
|
||
def make_doc (order, template, issue_id):
|
||
c = ConfigParser()
|
||
f = Template_dir+template
|
||
if c.read (f) == []:
|
||
raise Exception ('Не найден конигурационный файл шаблона: {f}'.format(f=f))
|
||
|
||
with Image.open(Template_dir + c.get('print', 'BackFileName')).convert("RGBA") as im:
|
||
print(im.format, im.size, im.mode)
|
||
im_txt = Image.new("RGBA", im.size, (255,255,255,0))
|
||
draw = ImageDraw.Draw(im_txt)
|
||
for fi in dict (c):
|
||
if fi in {'DEFAULT', 'print'}:
|
||
continue
|
||
|
||
if c.get(fi,'type') == 'serial':
|
||
text = get_serial (str(issue_id))
|
||
elif c.get(fi,'type') == 'string':
|
||
text = order[c.get(fi,'source')]
|
||
fsize = c.getint(fi,'size')
|
||
font = ImageFont.truetype(FontsDir[c.get(fi,'font')], fsize)
|
||
anchorX = c.get(fi,'anchor.X')
|
||
|
||
text_w, text_h = draw.textsize(text, font=font)
|
||
msg_y = c.getint(fi,'anchor.Y') - text_h
|
||
anchor = c.get(fi,'anchor')
|
||
if anchor == 'LL':
|
||
try:
|
||
msg_x = int(anchorX)
|
||
except:
|
||
raise TypeError ('Так как anchor = LL, то anchorX должен быть целым, а не {x}'.format(x=anchorX))
|
||
elif anchor == 'center':
|
||
if anchorX == 'center':
|
||
msg_x = int (im.size[0] / 2 - text_w/2)
|
||
else:
|
||
try:
|
||
msg_x = int (anchorX - text_w/2)
|
||
except:
|
||
raise TypeError ('anchorX={x}. Должен быть либо "сenter", либо целое'.format(x=anchorX))
|
||
else:
|
||
raise Exception ('Некорректный параметр anchor: {x}'.format(x=anchor))
|
||
|
||
#print ('anchorX =', anchorX)
|
||
#print ('text_w =', text_w)
|
||
#print ('im.size[0]= ', im.size[0])
|
||
#print ("msg_x, msg_y = ", msg_x, msg_y)
|
||
draw.text((msg_x, msg_y), text, font=font, fill='black')
|
||
|
||
doc_text_fn = 'Текст' + c.get('print', 'DocName')+ '-' + str(issue_id) + '.png'
|
||
doc_img_fn = c.get('print', 'DocName') + '-' + str(issue_id) + '.png'
|
||
dir = config.get('Templates', 'Create_doc_dir') # lpe.ini
|
||
|
||
im_txt.save(dir + doc_text_fn)
|
||
Image.alpha_composite(im, im_txt).save(dir + doc_img_fn)
|
||
result = [
|
||
{'path':dir + doc_text_fn, 'filename':doc_text_fn, 'description':'Текст-Свидетельство({x})'.format(x=template), 'content_type':'image/png'}, \
|
||
{'path':dir + doc_img_fn, 'filename':doc_img_fn, 'description':'Свидетельство({x})'.format(x=template), 'content_type':'image/png'}
|
||
]
|
||
|
||
return (result)
|
||
|
||
|
||
# код выполняемый при импорте модуля
|
||
|
||
config = ConfigParser()
|
||
if config.read(CONFIG_FILE_NAME) == []:
|
||
raise Exception ('Не найден файл конфигурации: {f}'.format(f=CONFIG_FILE_NAME))
|
||
|
||
Template_dir = config.get('Templates','Template_dir')
|
||
FontsDir = dict(config['FontsLocation'])
|
||
|
||
#order = {'student_fio':'Иванов Артем Сидорович', 'tutor_fio':"Кохлер Симеон Агарович", \
|
||
# 'issuedate':'20.02.2020', 'kursname':'Виртуализация в ОС Альт'
|
||
# }
|
||
#make_doc(order, 'svid_yellow', 1187)
|