按模板复制多个标签,并在标签中填入对应的文本及二维码

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
import os
import qrcode
import openpyxl
from openpyxl.drawing.image import Image

# 制作二维码
def gen_image(text,name):
img = qrcode.make(text)
with open(name,"wb") as f:
img.save(f)

# 写入二维码
def write_img(img_name,position):
imgsize = (720/12,720/12)
img = Image(img_name)
img.width,img.height = imgsize
wb[str(i)].add_image(img,position)

# 查找excel文件
def excel_file():
path = os.getcwd()
files = os.listdir(path)
fullpath = [f for f in files if not f.startswith(("~$")) and f.endswith((".xlsx"))]
return os.path.join(path,fullpath[0])

if __name__ == "__main__":
# 打开excel工作薄
wb = openpyxl.load_workbook(excel_file())
# 打开excel工作表清单
QD_sheet = wb.worksheets[0]
# 打开excel工作表标签模板
BQ_sheet = wb.worksheets[1]

if not os.path.exists("qrcode"):
os.mkdir("qrcode")

for i in range(1,10):
# 复制模板工作表
new_sheet = wb.copy_worksheet(BQ_sheet)
# 重命名新工作表
new_sheet.title = str(i)
# 将清单中的SN复制到新工作表指定单元格
text = QD_sheet['A' + str(i+1)].value
new_sheet['B7'].value = text

# 指定二维码存放位置
text_png_name = "qrcode/" + "A" + str(i+1) + ".png"
# 指定二维码粘贴位置
paste = "A7"
# 按清单列表制作二维码并存放到指定位置
gen_image(text,text_png_name)
# 将二维码放置到新工作表指定单元格
write_img(text_png_name,paste)

#保存excel文件
wb.save(excel_file())