批量制作二维码标签

从上往下贴图

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
import os,re
from datetime import date, time, timedelta
import qrcode as qr
import yagmail as yag
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
# 指定二维码的格式:box_size:表示每个方格带像素点个数,border:表示边距占位几个方格
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")
# # 获取生成的二维码图片尺寸:
# icon_w, icon_h = img.size
# icon = creat_icon(sq)
# img.paste(icon, (100, 100), icon)
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

# -----创建一张标签纸大小的空白照片,每英寸300像素的分辨率,标签纸尺寸 80 *270 mm
w = int(80 / 25.4 * 300)
h = int(270 / 25.4 * 300)
pg = Image.new("RGBA", (w, h))

# -----设置页边距,每行并排2张二维码-----
a = int((40 / 25.4 * 300 - w1) / 2)
b = int((27 / 25.4 * 300 - h1) / 2)

#-----粘贴二维码图片到标签纸上
x, y= a, b
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:
# print(page, i, x, y)
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" # SMTP服务器
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>&nbsp;</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,
# cc=ccs,
# bcc=bccs,
subject=title,
contents=html,
attachments=attachs)
print("Email send success")
except:
print("Email send fail")
if __name__ == '__main__':
name = input("输入编码! ")
num = int(input("输入要打印的多少张二维码! "))
print(type(num))
rq = f"{date.today():%y%m%d}"
remove_old()
creat_ewm(name,num,rq)
creat_tag(name,rq)
# send_email(name,num,rq)

从下往上贴图

  1. 符合打印机的习惯
  2. 不足一页时,可以省纸张
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
# 指定二维码的格式:box_size:表示每个方格带像素点个数,border:表示边距占位几个方格
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")
# # 获取生成的二维码图片尺寸:
# icon_w, icon_h = img.size
# icon = creat_icon(sq)
# img.paste(icon, (100, 100), icon)
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

# -----创建一张标签纸大小的空白照片,每英寸300像素的分辨率,标签纸尺寸 80 *270 mm
w = int(80 / 25.4 * 300)
h = int(270 / 25.4 * 300)
pg = Image.new("RGBA", (w, h))

# -----设置页边距,每行并排2张二维码-----
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:
# print(page, i, x, y)
pg.paste(img, (x, y))
x += w1 + a*2
else:
x = a
y -= h1 + b *2
if h + y - h1 > 0:
# print(page, i, x, y)
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
# print(page, i, x, y)
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" # SMTP服务器
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>&nbsp;</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,
# cc=ccs,
# bcc=bccs,
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)
# send_email(name,num,rq)