92 lines
2.3 KiB
Python
92 lines
2.3 KiB
Python
|
#!/usr/bin/env python3
|
||
|
|
||
|
import os.path
|
||
|
import qrcode
|
||
|
import qrcode.image.svg
|
||
|
import svgutils.transform as st
|
||
|
|
||
|
|
||
|
class qr_rechnung():
|
||
|
data = ''
|
||
|
error_correction = None
|
||
|
|
||
|
def __init__(self, data=None, textfile=None):
|
||
|
if textfile is not None:
|
||
|
self.set_data_from_textfile(textfile)
|
||
|
|
||
|
def set_data(self, data):
|
||
|
self.data = data
|
||
|
|
||
|
def set_data_from_textfile(self, textfile):
|
||
|
with open(textfile, 'r') as f:
|
||
|
self.data = f.read()
|
||
|
|
||
|
def write_file(self, filepath, force=False):
|
||
|
global qrcode
|
||
|
mode = 'w' if force else 'x'
|
||
|
try:
|
||
|
assert force or not os.path.isfile(filepath)
|
||
|
factory = qrcode.image.svg.SvgPathImage
|
||
|
img = qrcode.make(
|
||
|
self.data,
|
||
|
error_correction=qrcode.constants.ERROR_CORRECT_M,
|
||
|
image_factory=qrcode.image.svg.SvgPathImage
|
||
|
)
|
||
|
img.save(filepath)
|
||
|
except AssertionError:
|
||
|
print(f"ERROR: Datei {filepath} existiert bereits.")
|
||
|
return 1
|
||
|
|
||
|
qrcode = st.fromfile(filepath)
|
||
|
offset = self.calc_move_offset(qrcode.get_size()[0])
|
||
|
swisscross = st.fromfile("CH-Kreuz_7mm.svg").getroot()
|
||
|
swisscross.moveto(
|
||
|
x=offset['offset'],
|
||
|
y=offset['offset'],
|
||
|
scale_x=offset['scale'],
|
||
|
scale_y=offset['scale']
|
||
|
)
|
||
|
qrcode.append(swisscross)
|
||
|
qrcode.save(filepath)
|
||
|
|
||
|
def calc_move_offset(self, size):
|
||
|
org = float(size[:-2])
|
||
|
return {
|
||
|
'offset': org*0.427935156,
|
||
|
'scale': org/137.364857143
|
||
|
}
|
||
|
|
||
|
|
||
|
def help(args):
|
||
|
return '{0}: Ungültige Eingabe\nAufruf: \
|
||
|
{0} TEXTDATEI [SVG-ZIELDATEI]'.format(args[0])
|
||
|
|
||
|
|
||
|
def main(args):
|
||
|
try:
|
||
|
textfile = args[1]
|
||
|
except IndexError:
|
||
|
print(help(args))
|
||
|
return 1
|
||
|
try:
|
||
|
svgfile = args[2]
|
||
|
except IndexError:
|
||
|
try:
|
||
|
assert textfile[-4:] == '.txt'
|
||
|
svgfile = textfile[:-4]+'.svg'
|
||
|
except AssertionError:
|
||
|
print(help(args))
|
||
|
return 1
|
||
|
try:
|
||
|
x = qr_rechnung(textfile=textfile)
|
||
|
x.write_file(svgfile, force=False)
|
||
|
except (PermissionError, FileNotFoundError) as e:
|
||
|
print(e)
|
||
|
return 1
|
||
|
return 0
|
||
|
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
import sys
|
||
|
sys.exit(main(sys.argv))
|