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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
| import os,random,time import qrcode from pystrich.ean13 import EAN13Encoder
number = "1234567890" letter = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
def mainmenu(): '''企业编码管理系统主菜单''' print("""\033[1;35m **************************************************************** 企业编码生成系统 **************************************************************** 1.生成6位数字防伪编码 (213563型) 2.生成9位系列产品数字防伪编码(879-335439型) 3.生成25位混合产品序列号(B2R12-N7TE8-9IET2-FE35O-DW2K4型)
4.生成含数据分析功能的防伪编码(5A61M053D) 5.智能批量生成带数据分析功能的防伪码 6.后续补加生成防伪码
7.EAN-13条形码批量生成 8.二维码批量输出 0.退出系统 ================================================================ 说明:通过数字键选择菜单 ================================================================ \033[0m""")
def input_validation(insel): '''判断在主菜单的输入是否非法''' if str.isdigit(insel): insel = int(insel) return insel else: print("输入非法,请重新输入") return 0
def mkdir(path): '''创建文件夹''' isexists = os.path.exists(path) if not isexists: os.mkdir(path)
def openfile(filename): '''读取文本文件''' f = open(filename) fllist = f.read() f.close() return fllist
def inputbox(showstr, showorder, length): """ 输入验证判断 :param showstr: 输入提示文字 :param showorder: 输入内容的类型,1.数字,不限位数,2.字母,3.数字且有位数要求 :param length: 输入内容的长度 :return: """ instr = input(showstr) if len(instr) != 0: if showorder == 1: if str.isdigit(instr): if instr == 0: print("输入为0,请重新输入!") return '0' else: return instr else: print("输入非法,请重新输入!") return '0' if showorder == 2: if str.isalpha(instr): if len(instr) != length: print("必须输入" + str(length) + "个字母,请重新输入!") return '0' else: return instr if showorder == 3: if str.isdigit(instr): if len(instr) != length: print("必须输入" + str(length) + "个数字,请重新输入!") return '0' else: return instr else: print("输入非法,请重新输入!") return '0' else: print("输入为空,请重新输入!") return '0'
def wfile(sstr, sfile, typeis, smsg, datapath): """ 编码输出显示,同时保存文件 :param sstr: 生成的防伪码 :param sfile: 保存防伪码的文件名 :param typeis: 是否显示输出完成的防伪码,""时显示提示框,"no"时不显示 :param smsg: 提示框显示的提示内容 :param datapath: 保存防伪码的路径 """ mkdir(datapath) datafile = datapath + '\\' + sfile file = open(datafile, 'w') wrlist = sorted(list(sstr)) pdata = "" wdata = "" for i in range(len(wrlist)): wdata = str(wrlist[i].replace('[', '').replace(']', '')) wdata = wdata.replace('"', '').replace("'", "") file.write(str(wdata)) pdata = pdata + wdata file.close()
if typeis != "no": print(pdata)
print(smsg + str(len(wrlist)) + "\n防伪码文件存放位置" + datafile)
def replace_strIndex(string, char, index): '''将字符串中指定位置的字符进行替换''' string = list(string) string[index] = char return "".join(string)
def scode1(schoice): '''生成6位数字防伪编码 (213563型)''' incount = inputbox("\033[1;32m 请输入您要生成验证码的数量:\33[0m", 1, 0) while int(incount) == 0: incount = inputbox("\033[1;32m 请输入您要生成验证码的数量:\33[0m", 1, 0)
coding_set = set()
while len(coding_set) != int(incount): coding = "" for k in range(6): coding = coding + random.choice(number) coding = coding + "\n" coding_set.add(coding)
wfile(coding_set, "scode" + str(schoice) + ".txt", "", "已生成6位防伪码共计:", "codepath")
def scode2(schoice): '''生成9位数字系列产品防伪编码''' beginNum = inputbox("\033[1;32m 请输入系列产品的数字起始号码(3位):\33[0m", 3, 3) while int(beginNum) == 0: beginNum = inputbox("\033[1;32m 请输入系列产品的数字起始号码(3位):\33[0m", 3, 3) beginNum = int(beginNum)
kindNum = inputbox("\033[1;32m 请输入产品系列的数量:\33[0m", 1, 0) while int(kindNum) == 0: kindNum = inputbox("\033[1;32m 请输入产品系列的数量:\33[0m", 1, 0) kindNum = int(kindNum)
aloneNum = inputbox("\033[1;32m 请输入要生成的每个系列产品的防伪码数量:\33[0m", 1, 0) while int(aloneNum) == 0: aloneNum = inputbox("\033[1;32m 请输入要生成的每个系列产品的防伪码数量:\33[0m", 1, 0) aloneNum = int(aloneNum)
coding_set = set()
for j in range(kindNum): beginNum = beginNum + 1 litter_coding_set = set() while len(litter_coding_set) != aloneNum: coding = str(beginNum) for m in range(6): coding = coding + random.choice(number) coding = coding + "\n" litter_coding_set.add(coding) coding_set.update(litter_coding_set)
wfile(coding_set, "scode" + str(schoice) + ".txt", "", "已生成9位系列产品防伪码共计:", "codepath")
def scode3(schoice): '''生成25位混合产品序列号''' incountNum = inputbox("\033[1;32m 请输入要生成的25位混合产品序列号数量:\33[0m", 1, 0) while int(incountNum) == 0: incountNum = inputbox("\033[1;32m 请输入要生成的25位混合产品序列号数量:\33[0m", 1, 0) incountNum = int(incountNum)
coding_set = set()
while len(coding_set) != incountNum: randstrtmp = "" for k in range(5): coding = "" for m in range(5): coding = coding + random.choice(letter) if k == 4: coding = coding + "\n" else: coding = coding + "-"
randstrtmp = randstrtmp + coding coding_set.add(randstrtmp)
wfile(coding_set, "scode" + str(schoice) + ".txt", "", "已生成25位混合产品序列号共计:", "codepath")
def scode4(schoice): '''生成含数据分析功能的防伪编码 主要由3位字母和6位数字组成,并且字母位置随机,但是首字母对应不同地区, 次字母对应产品颜色,最后的字母对应产品批次 ''' inLetter = inputbox("\033[1;32m 请输入数据分析编号的三位字母:\33[0m", 2, 3) while not inLetter.isalpha() or len(inLetter) != 3: inLetter = inputbox("\033[1;32m 请输入数据分析编号的三位字母:\33[0m", 2, 3)
incountNum = inputbox("\033[1;32m 请输入要生成的带数据分析功能的防伪码数量:\33[0m", 1, 0) while int(incountNum) == 0: incountNum = inputbox("\033[1;32m 请输入要生成的带数据分析功能的防伪码数量:\33[0m", 1, 0) incountNum = int(incountNum)
coding_set = set()
let = [inLetter[j].upper() for j in range(3)]
while len(coding_set) != incountNum: indexNum = "012345678" ranIndex = random.sample(indexNum, 3) ranIndex = list(map(int, ranIndex)) ranIndex = sorted(ranIndex)
myNum = "" for k in range(9): myNum = myNum + random.choice(number)
for n in range(3): myNum = replace_strIndex(myNum, let[n], ranIndex[n])
myNum = myNum + "\n" coding_set.add(myNum)
wfile(coding_set, "scode" + str(schoice) + ".txt", "", "已生成9位含数据分析功能的防伪编码共计:", "codepath")
def scode5(schoice): ''' 智能批量生成带数据分析功能的防伪码 利用文件批量生成数据 ''' file_path = "plcode.mri" codelist = openfile(file_path) codelist = codelist.split("\n")
for item in codelist: codea = item.split(",")[0] codeb = item.split(",")[1]
coding_set = set()
let = [codea[j].upper() for j in range(len(codea))]
while len(coding_set) != int(codeb): indexNumber = "012345678" ranIndex = random.sample(indexNumber, 3) ranIndex = list(map(int, ranIndex)) ranIndex = sorted(ranIndex)
myNum = "" for k in range(9): myNum = myNum + random.choice(number)
for m in range(3): myNum = replace_strIndex(myNum, let[m], ranIndex[m])
myNum = myNum + "\n" coding_set.add(myNum) wfile(coding_set, codea + "scode" + str(schoice) + ".txt", "no", "已生成9位含数据分析功能的防伪编码共计:", "codepath")
def scode6(schoice): '''后续补加生成防伪码''' print("\033[1;32m 本功能只针对于功能5的数据文件做添加操作!:\33[0m") file_name = input("请输入要补加防伪码的文件名称:") file_path = r"./codepath/" + file_name fileData = openfile(file_path) fileData_list = fileData.split("\n") fileData_list.pop()
for i in range(len(fileData_list)): fileData_list[i] = fileData_list[i] + "\n"
print("之前生成的防伪码数量共计:" + str(len(fileData_list)) + "个")
fileData_set = set(fileData_list)
incountNum = inputbox("\033[1;32m 请输入要补加的带数据分析功能的防伪码数量:\33[0m", 1, 0) while int(incountNum) == 0: incountNum = inputbox("\033[1;32m 请输入要补加的带数据分析功能的防伪码数量:\33[0m", 1, 0) incountNum = int(incountNum)
let = [file_name[j].upper() for j in range(3)]
while len(fileData_set) - len(fileData_list) != incountNum: indexNumber = "012345678" ranIndex = random.sample(indexNumber, 3) ranIndex = list(map(int, ranIndex)) ranIndex = sorted(ranIndex)
myNum = "" for k in range(9): myNum = myNum + random.choice(number)
for m in range(3): myNum = replace_strIndex(myNum, let[m], ranIndex[m])
myNum = myNum + "\n" fileData_set.add(myNum)
print("已经新生成的防伪码数量共计:" + str(len(fileData_set) - len(fileData_list)) + "个")
wfile(fileData_set, file_name[:3] + "scode" + str(schoice) + ".txt", "no", "已生成9位含数据分析功能的防伪编码共计:", "codepath")
def scode7(schoice): ''' 实现条形码的输出 EAN-13是一种常见的条形码,多用于超市和零售市场 EAN-13: European Article Number,共计13位 前3位为国家码(中国大陆是690-695),4-7位为企业码,8-12位为商品码,第13位是校验位 校验位的计算: 1、前12位的偶数位相加 2、前12位的奇数位相加 3、将偶数位的和的三倍与奇数位的和相加 4、取出得到的和的个位 5、由10减去这个个位数,再将得到的结果取个位即可 '''
nationId = inputbox("\033[1;32m 请输入EAN-13的国家码(3位):\33[0m", 3, 3) while int(nationId) == 0: nationId = inputbox("\033[1;32m 请输入EAN-13的国家码(3位):\33[0m", 3, 3)
compId = inputbox("\033[1;32m 请输入EAN-13的企业码(4位):\33[0m", 3, 4) while int(compId) == 0: compId = inputbox("\033[1;32m 请输入EAN-13的企业码(4位):\33[0m", 3, 4)
codeNum = inputbox("\033[1;32m 请输入要生成的条形码的数量 :\33[0m", 1, 0) while int(codeNum) == 0: codeNum = ("\033[1;32m 请输入要生成的条形码的数量 :\33[0m", 1, 0)
codeNum = int(codeNum)
codeNum_set = set() while len(codeNum_set) != codeNum:
sellId = "" for j in range(5): sellId = sellId + random.choice(number)
sellId = nationId + compId + sellId oushu = 0 for k in range(0,11,2): oushu = oushu + int(sellId[k]) jishu = 0 for k in range(1,11,2): jishu = jishu + int(sellId[k])
jiaoyan = (10 - (oushu*3 + jishu)%10)%10
sellId = sellId + str(jiaoyan) codeNum_set.add(sellId)
wfile(codeNum_set, "scode" + str(schoice) + ".txt", "no", "已生成EAN-13条形码共计:", "codepath") codeNum_list = list(codeNum_set) mkdir("ean13code") for index in range(codeNum): encoder = EAN13Encoder(codeNum_list[index]) encoder.save("ean13code\\"+codeNum_list[index]+".png")
print("ean-13的全部条形码保存在"+"ean13code文件夹下")
def scode8(schoice): codeNum = inputbox("\033[1;32m 请输入要生成的12位数字二维码的数量 :\33[0m", 1, 0) while int(codeNum) == 0: codeNum = ("\033[1;32m 请输入要生成的条形码的数量 :\33[0m", 1, 0) codeNum = int(codeNum)
codeNum_set = set() while len(codeNum_set) != codeNum: qrRanCode = "" for j in range(12): qrRanCode = qrRanCode + random.choice(number)
codeNum_set.add(qrRanCode)
wfile(codeNum_set, "scode" + str(schoice) + ".txt", "no", "已生成12位数字二维码共计:", "codepath") mkdir("qrcode") codeNum_list = list(codeNum_set)
for j in range(len(codeNum_list)): encoder = qrcode.make(codeNum_list[j]) encoder.save("qrcode\\"+codeNum_list[j]+".png") return True
def mainpro(): ''' 通过循环控制用户对程序功能的选择 ''' while True: mainmenu() choice = input("\033[1;32m 请输入您要操作的菜单选项:\33[0m") if len(choice) != 0: choice = input_validation(choice) if choice == 1: scode1(choice) if choice == 2: scode2(choice) if choice == 3: scode3(choice) if choice == 4: scode4(choice) if choice == 5: scode5(choice) if choice == 6: scode6(choice) if choice == 7: scode7(choice) if choice == 8: scode8(choice) if choice == 0: print("正在退出系统!!") break else: print("\033[1;31;40m 输入非法,请重新输入!!\033[0m") time.sleep(2)
mainpro()
|