1
0
Fork 0

Erster Commit

This commit is contained in:
acn 2019-08-15 15:17:19 +02:00
parent 61accc5410
commit 31b61fc078
2 changed files with 120 additions and 2 deletions

View File

@ -1,3 +1,19 @@
# etikettenpost
# Etikettenpost
Ein Programm zur Konvertierung von DHL-Paketmarken und ePost-Briefmarken in PDFs, die mit einem Brother-Etikettendrucker ausgedruckt werden können.
Aktuell funktioniert es für folgende Formate:
* ''DHL-Paketmarke'' von paket.de
* ''ePost-Briefmarke'' von deutschepost.de
Weitere Formate (DHL-Rücksendungen, Amazon Rücksendungen) kommen hinzu, sobald ich sie brauche :)
# Voraussetzungen
* Python 3
* python3-wand
* python3-reportlab
* python3-numpy
Ein Programm zur Konvertierung von DHL-Paketmarken und ePost-Briefmarken in PDFs, die mit einem Brother-Etikettendrucker ausgedruckt werden können.

102
post.py Executable file
View File

@ -0,0 +1,102 @@
#!/usr/bin/python3
import wand.image
import numpy
import PIL
import io
from argparse import ArgumentParser, FileType
from reportlab.pdfgen import canvas
from reportlab.lib.units import inch, cm, mm
#from reportlab.platypus import SimpleDocTemplate, Image
# Hilfsfunktionen:
def getPIL(bild):
# wandelt ein wand.image.Image in ein PIL.Image um
# damit kann ein PDF-Schnipsel direkt in einen ReportLab-Canvas eingefügt werden
img_buffer = numpy.asarray(bytearray(bild.make_blob(format='png')), dtype='uint8')
return PIL.Image.open(io.BytesIO(img_buffer))
def details(bild):
# gibt zu einem wand.image.Image die Größe und das Verhältnis aus
return "Größe: " + str(bild.size) + " Ratio: " + str(bild.width/bild.height)
def getH(bild, w):
# um das Seitenverhältnis beizubehalten:
# gibt zu gegebener Breite die passende Höhe zurück
return w / (bild.width/bild.height)
def getW(bild, h):
# um das Seitenverhältnis beizubehalten:
# gibt zu gegebener Höhe die passende Breite zurück
return h / (bild.height/bild.width)
def makeDHLPaketmarke(eingabe, ausgabe):
# Erstellt ein Paketetikett von einer DIN A4-DHL-Paketmarke
# Standard DHL-Paketmarke:
# Bild 90° nach rechts drehen, dann folgende Koordinaten:
# - 2045x108, 2533x204: obere Zeile "Paket ..."
# - 2873x121, 3205x173: DHL Logo
# - 2045x220, 2894x871: Absender+Empfänger
# - 2045x887, 2771x1047: GO GREEN/Sendungsnr.
# - 2306x1533, 2995x2358: Barcodes
print("DHL-Paketmarke")
with wand.image.Image(filename=eingabe, resolution=300) as inPdf:
inPdf.rotate(90)
ObereZeile = inPdf[2045:2533, 108:204]
DHLlogo = inPdf[2873:3205, 121:173]
AbsEmpf = inPdf[2045:2894, 220:871]
Sendungsnr = inPdf[2045:2771, 887:1047]
Sendungsnr.rotate(270)
Barcodes = inPdf[2306:2995, 1533:2358]
outPdf = canvas.Canvas(ausgabe, pagesize=(160 * mm, 62 * mm))
outPdf.drawInlineImage(getPIL(ObereZeile), 2*mm, 57*mm,
width = getW(ObereZeile,5)*mm, height=5*mm)
outPdf.drawInlineImage(getPIL(DHLlogo), 45*mm, 57*mm,
width = getW(DHLlogo,5)*mm, height=5*mm)
outPdf.drawInlineImage(getPIL(AbsEmpf), 3*mm, 0,
width = getW(AbsEmpf,55)*mm, height=55*mm)
outPdf.drawInlineImage(getPIL(Sendungsnr), 80*mm, 1*mm,
width = getW(Sendungsnr,60)*mm, height=60*mm)
outPdf.drawInlineImage(getPIL(Barcodes), 100*mm, 0,
width = getW(Barcodes,62)*mm, height=62*mm)
outPdf.save()
def makeBriefmarke(eingabe, ausgabe):
# Erstellt eine Briefmarke aus einer ePost-PDF-Datei
print("ePost-Briefmarke")
print("Eingabedatei: " + eingabe)
print("Ausgabedatei: " + ausgabe)
with wand.image.Image(filename=eingabe, resolution=300) as inPdf:
porto = inPdf[146:580, 32:266]
porto.rotate(270)
outPdf = canvas.Canvas(ausgabe, pagesize=(37*mm, 62*mm))
outPdf.drawInlineImage(getPIL(porto), 2*mm, 2*mm,
width = getW(porto,58)*mm, height=58*mm)
outPdf.save()
### main():
parser = ArgumentParser()
parser.add_argument("inputfile", help="Eingabedatei (DHL-PDF)")
parser.add_argument("-o", dest="output", metavar='OUTPUT', help="Ausgabedatei (PDF)")
parser.add_argument("-t", dest="type", choices=["DHL", "ePost"], default="DHL", help="Art der Eingabedatei (Standard: DHL)")
args = parser.parse_args()
if args.output:
outfile = args.output
else:
outfile = args.inputfile + "-out.pdf"
infile = args.inputfile
if args.type=="DHL":
makeDHLPaketmarke(infile, outfile)
elif args.type=="ePost":
makeBriefmarke(infile, outfile)