1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
| import os,re from datetime import date, time, timedelta import qrcode import yagmail from PIL import Image, ImageDraw, ImageFont
def remove_old(): """ 删除文件夹下的旧文件。webp格式的旧文件(二维码图片) """ fold = r"D:\onedrive\Code\Note\Python_project\WDHAC_QR" files = [i for i in os.listdir(fold) if re.search(r"webp$", i)] for file in files: os.remove(f"{fold}\\{file}")
def creat_icon(text): """ 生成在二维码图片中间添加的文字图片 """ icon = Image.new("RGBA", (50, 35), (0, 0, 0, 120)) draw = ImageDraw.Draw(icon) font = ImageFont.truetype(font="msyh.ttc", size=28) draw.text((0, 0), text, fill=(255, 255, 255, 255), font=font) return icon
def creat_ewm(name, num, rq): """ 批量生成二维码 参数: name(str) 物料名称 num(int) 数量 rq(str) 日期231001 23年10月1日 """ for i in range(1, int(num) + 1): if num > 99: sq = str(i).rjust(3, "0") data = name + rq + sq else: sq = str(i).rjust(2, "0") data = name + rq + sq qr = qrcode.QRCode(version=2, error_correction=qrcode.constants.ERROR_CORRECT_H, box_size=6, border=2) qr.add_data(data) qr.make(fit=True) img = qr.make_image() img = img.convert("RGBA") img.save(f"{rq}_{sq}.webp")
def creat_tag(name, rq): """ 生成可以打印的二维码标签.倒序打印,省纸 参数: name(str) 物料名称 rq(str) 日期231001 23年10月1日 """ files = [x for x in os.listdir() if re.search("^\d{6}_\d+", x)] img = Image.open(files[0]) w1, h1 = img.size w = int(80 / 25.4 * 300) h = int(270 / 25.4 * 300) pg = Image.new("RGBA", (w, h)) a = int((40 / 25.4 * 300 - w1) / 2) b = int((27 / 25.4 * 300 - h1) / 2) x, y= a, h-b-h1 page = 1 for i in files: img = Image.open(i) if w - x - w1 >= 0: pg.paste(img, (x, y)) x += w1 + a*2 else: x = a y -= h1 + b *2 if h + y - h1 > 0: pg.paste(img, (x, y)) x += w1 + a *2 else: pg.save("{}.webp".format(name + rq + "_" + str(page))) pg = Image.new("RGBA", (w, h)) x, y= a, b page += 1 pg.paste(img, (x, y)) x += w1 + a *2 img.close() pg.save("{}.webp".format(name + rq + "_" + str(page))) for x in files: os.remove(x)
def send_email(name, num, rq): """ 自动发送邮件。 参数: name(str): 物料名称 num(int): 数量 rq(str) 日期231001 23年10月1日 """ mail_host = "smtp.163.com" port = int(25) sender = "yxhsiao@163.com" pwd = "MGWMGHFEVMYQLFVK" receivers = ["zgjxjjwyx@126.com"] ccs = ["1796685160@qq.com"] bccs = ["yxhsiao@hotmail.com"] title = f"{date.today()} 发货:{name} 数量:{num} Pcs" html = "<p style='font: normal 150 13pt 黑体;color:#f104b7;text-align:center'><p><strong><span>二维码制作已完成下载即可!</span></strong></p><ol start='' ><li><p><span>标签打印机页面模板设置:</span></p><ul><li><span>纸张大小:80 x 270 </span></li><li><span>布局:上边距 2mm</span></li></ul></li><li><p><span>二维码图片大小设置:</span></p><ul><li><p><span>伸展到方框范围</span></p><ul><li><span>保持纵横比</span></li><li><span>尺寸:80 x 270</span></li></ul></li></ul></li><li><p><span>并设置图片左右上下居中</span></p></li></ol><p> </p>" attachs = [i for i in os.listdir() if re.match(r"{name}{rq}", i)] try: yag = yagmail.SMTP(user=sender, password=pwd, host=mail_host) yag.send( to=receivers, subject=title, contents=html, attachments=attachs) print("Email send success") except: print("Email send fail") if __name__ == '__main__': name = "WDH5NC00701" num = 6 print(type(num)) rq = f"{date.today():%y%m%d}" remove_old() creat_ewm(name,num,rq) creat_tag(name,rq)
|