1 import json
2 import qrcode
3 from PIL import Image, ImageDraw, ImageFont
4 # import matplotlib.pyplot as plt
5 from openpyxl import load_workbook
6
7
8 def info(name, body, num, bianma, tips):
9 '''
10 :param name: 保存图片名称,后缀需要png
11 :param body: 扫描显示的内容
12 :param num: 保管人
13 :param bianma: 资产编码
14 :param tips: 提示语
15 :return: Null
16 :根据参数内容创建一个带logo与文字的二维码图片
17 '''
18
19 qr = qrcode.QRCode(
20 version=1,
21 error_correction=qrcode.constants.ERROR_CORRECT_L,
22 box_size=10,
23 border=2,
24 )
25 # 添加数据
26 qr.add_data(body)
27 # 填充数据
28 qr.make(fit=True)
29 # 生成图片
30 img = qr.make_image(fill_color="black", back_color="white")
31 img = img.convert("CMYK") # RGBA
32 # 添加logo
33 icon = Image.open("logo.jpg")
34 # 获取图片的宽高
35 img_w, img_h = img.size
36 factor = 6
37 size_w = int(img_w / factor)
38 size_h = int(img_h / factor)
39 icon_w, icon_h = icon.size
40 if icon_w > size_w:
41 icon_w = size_w
42 if icon_h > size_h:
43 icon_h = size_h
44 # 重新设置logo的尺寸
45 icon = icon.resize((icon_w, icon_h), Image.ANTIALIAS)
46 w = int((img_w - icon_w) / 2)
47 h = int((img_h - icon_h) / 2)
48
49 img.paste(icon, (w, h), None)
50 # 显示图片
51 # plt.imshow(img)
52 # plt.show()
53 img = img.convert('RGBA')
54 img.save(name)
55 # img = img
56 oriImg = Image.open("ditu.jpg") # 打开底图
57 oriImg2 = Image.open(name) # 打开新建的二维码图
58 oriImg2 = oriImg2.resize((250, 250)) # 设置二维码大小
59 oriImg.paste(oriImg2, (82, 50)) # 将二维码 放在底图什么未知 距顶点的右(98) 上(195)
60 draw = ImageDraw.Draw(oriImg) # 底图加二维码图
61 # print(draw)
62 font = ImageFont.truetype('simhei.ttf', 25) # 设置图片上文本信息字体(需要使用中文字体,不然中文会乱码)
63 font2 = ImageFont.truetype('simhei.ttf', 16) # 设置图片上文本信息字体(需要使用中文字体,不然中文会乱码)
64 draw.text((150, 11), tips, (50, 51, 51), font=font2)
65 draw.text((70, 310), '资产编码:' + bianma, (50, 51, 51), font=font) # 把字添加到图片上 文本位置 距顶点的右(100) 上(425) '保管人:' + num
66 draw.text((165, 355), num, (50, 51, 51), font=font) # 把字添加到图片上 文本位置 距顶点的右(100) 上(425)
67 oriImg = oriImg.convert('RGBA')
68 oriImg.save(name) #
69
70
71 def main():
72 assets_dict = {"zichanbianma": "资产编码:", "tips": "请爱护公司财产"}
73 wb = load_workbook("itassets.xlsx")
74 sh = wb["Sheet1"]
75 rows = sh.max_row
76 columns = sh.max_column
77 for i in range(2, rows + 1):
78 core_list = []
79 for j in range(1, columns + 1):
80 str = sh.cell(i, j).value
81 core_list.append(str)
82 assets_qrpathname = core_list[1] + ".png"
83 assets_username = core_list[4]
84 assets_num = core_list[1]
85 # 字典格式化下数据
86 assets_core = {
87 "序号" : core_list[0],
88 "资产编码": core_list[1],
89 "资产名称": core_list[2],
90 "购置时间": core_list[3],
91 "保管人": core_list[4],
92 "归属部门": core_list[5],
93 "审核人": core_list[6],
94 "电脑名称": core_list[7],
95 "操作系统": core_list[8],
96 "P显示器1": core_list[9],
97 "P显示器2": core_list[10],
98 "主板型号": core_list[11],
99 "显卡型号": core_list[12],
100 "CPU型号": core_list[13],
101 "内存容量(GB)": core_list[14],
102 "硬盘容量(GB)": core_list[15],
103 }
104 # json 格式化字典 ensure_ascii=False 处理中文
105 assets_core = json.dumps(assets_core, indent=3, ensure_ascii=False)
106 info(assets_qrpathname, assets_core, assets_username, assets_num, assets_dict["tips"])
107
108
109 if __name__ == '__main__':
110 main()