一、文件夾結構
二、相關說明
1、系統開發環境
windows10系統,python3.6,開放工具ptcharm2018
2、系統功能結構
1.生產6位數字防僞編碼(213563型)
2.生成9位系列產品數字防僞編碼(879-33577型)
3.生成25位混合產品序列號(R2R12-M7TY3-GH35O-DW2K8)
4.生成含數據分析功能的防僞編碼(1245M9569D3)
5.智能批量生產帶數據分析功能的防僞編碼
6.後續補加生成防僞碼(5A64M5621A3)
7.EAN-13條形碼批量生成
8.二維碼批量輸出
9.企業粉絲防僞碼抽籤
3、小說明
還有很多不足,有想法的歡迎的評論區提出,對代碼不理解的也可以提出
4、相關代碼
# _*_ coding:utf-8 _*_
import os, time, string, random, tkinter, qrcode
from pystrich.ean13 import EAN13Encoder
import tkinter.filedialog # 文件對話框,其中tkinter模塊爲python的標準圖形化界面接口
import tkinter.messagebox # 消息對話框
from tkinter import *
from string import digits
root = tkinter.Tk() # 建立根窗口
# 初始化數據
number = "1234567890"
letter = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
allis = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!@#$%^&*()_+"
i = 0
randstr = []
fourth = []
fifth = []
randfir = ""
randsec = ""
randthr = ""
str_one = ""
strone = ""
strtwo = ""
nextcard = ""
userput = ""
nres_letter = ""
def mainmenu():
print("""\033[1;34m
**********************************************************
企業編碼生產系統
**********************************************************
1.生產6位數字防僞編碼(213563型)
2.生成9位系列產品數字防僞編碼(879-33577型)
3.生成25位混合產品序列號(R2R12-M7TY3-GH35O-DW2K8)
4.生成含數據分析功能的防僞編碼(1245M9569D3)
5.智能批量生產帶數據分析功能的防僞編碼
6.後續補加生成防僞碼(5A64M5621A3)
7.EAN-13條形碼批量生成
8.二維碼批量輸出
9.企業粉絲防僞碼抽籤
0.退出系統
**********************************************************
說明:通過數字鍵選擇菜單
**********************************************************
\033[0m""")
def input_validation(insel):
if str.isdigit(insel):
if insel == "0":
print("\033[1;31;40m 注意:即將退出系統!!!\033[0m")
return "0"
else:
return insel
else:
print("\033[1;31;40m 非法輸入,請重新輸入!!!\033[0m")
return 0
def scode1(schoice):
# 調用inputbox函數對輸入數據進行非空、輸入合法性判斷
incount = inputbox("\033[1;32m 請輸入您要生成驗證碼的數量:\33[0m", 1, 0)
while int(incount) == 0: # 如果輸入爲字母或數字0,則要求重新輸入
incount = inputbox("\033[1;32m 請輸入您要生成驗證碼的數量:\33[0m", 1, 0)
randstr.clear() # 清空保存批量註冊碼信息的變量randstr
for j in range(int(incount)): # 根據輸入的驗證碼數量循環批量生成註冊碼
randfir = '' # 設置存儲單條註冊碼的變量爲空
for i in range(6): # 循環生成單條註冊碼
randfir = randfir + random.choice(number) # 產生數字隨機因子
randfir = randfir + "\n" # 在單條註冊碼後面添加轉義換行字符“\n”,使驗證碼單條列顯示
randstr.append(randfir) # 將單條註冊碼添加到保存批量驗證碼的變量randstr
# 調用函數wfile(),實現生成的防僞碼屏幕輸出和文件輸出
wfile(randstr, "scode" + str(schoice) + ".txt", "", "已生成6位防僞碼共計:", "codepath")
def scode2(schoice):
ordstart = inputbox("\033[1;32m 請輸入系列產品的數字起始號(3位):\033[0m", 3, 3)
# 如果輸入非法,則要求重新輸入
while int(ordstart) == 0:
ordstart = inputbox("\033[1;32m 請輸入系列產品的數字起始號(3位):\033[0m", 3, 3)
ordcount = inputbox("\033[1;32m 請輸入系列產品的數量:\033[0m", 1, 0)
# 如果輸入的產品序列號小於1或者大於9999,則要求重新輸入
while int(ordcount)<1 or int(ordcount)>9999:
ordcount = inputbox("\033[1;32m 請輸入系列產品的數量:\033[0m", 1, 0)
incount = inputbox("\033[1;32m 請輸入要生成的每個系列產品的防僞碼數量:\033[0m", 1, 0)
while int(incount) == 0:
incount = inputbox("\033[1;32m 請輸入要生成的每個系列產品的防僞碼數量:\033[0m", 1, 0)
randstr.clear() # 清空保存批量防僞碼信息的變量
for m in range(int(ordcount)): # 分類產品編號
for j in range(int(incount)): # 產品防僞碼編號
randfir =""
for i in range(6): # 生成一個不包含類別的產品防僞碼
randfir = randfir + random.choice(number) # 每次生成一個隨機因子
# 將生產的單條防僞碼添加到防僞碼列表
randstr.append(str(int(ordstart) + m) + randfir + "\n")
# 調用wfile函數,實現生產的防僞碼在屏幕輸出和文件
wfile(randstr, "scode" + str(schoice) + ".txt", "", "已經生成9位系列產品防僞碼共計:", "codepath")
# 生成25位混合產品序列號的函數,參數schoice設置輸出的文件名稱
def scode3(schoice):
# 輸入要生成的防僞碼數量
incount = inputbox("\033[1;32m 請輸入要生成的25位混合產品序列號數量:\033[0m", 1, 0)
while int(incount) == 0: # 如果輸入非法,則重新輸入
incount = inputbox("\033[1;32m 請輸入要生成的25位混合產品序列號數量:\033[0m", 1, 0)
randstr.clear() # 清空保存批量防僞碼信息的變量
for j in range(int(incount)): # 按輸入的數量生成防僞碼
strone = "" # 保存生成的單條防僞碼,不帶橫向,循環時清空
for i in range(25):
# 每次隨機生成一個因子,也就是每次產生單條防僞碼的一位
strone = strone +random.choice(letter)
# 將生產的防僞碼每隔5位添加橫向
strtwo = strone[:5] + "-" + strone[5:10] + "-" + strone[10:15] + "-" + strone[15:20] + "-" + strone[20:25] + "\n"
randstr.append(strtwo) # 添加防僞碼到防僞碼列表
# 調用函數wfile(),實現生產的防僞碼在屏幕輸出和文件輸出
wfile(randstr, "scode" + str(schoice) + ".txt", "", "已生成25位混合防僞序列碼共計:", "codepath")
# 生成含數據分析功能防僞碼函數,參數schoice設置輸出的文件格式
def scode4(schoice):
intype = inputbox("\033[1;32m 請輸入數據分析編號(3位字母) :\033[0m", 2, 3)
# 驗證輸入是否是三個字母,所以要判斷是否是字母和輸入長度是否爲3
while not str.isalpha(intype) or len(intype) != 3:
intype = inputbox("\033[1;32m 請輸入數據分析編號(3位字母) :\033[0m", 2, 3)
incount = inputbox("\033[1;32m 輸入要生成的帶數據分析功能的防僞碼數量:\033[0m", 1, 0)
# 驗證輸入是否大於零的整數,方法是判斷輸入轉換爲整數值時是否大於0
while int(incount) == 0: # 如果轉換爲整數時,需要重新輸入
incount = inputbox("\033[1;32m 輸入要生成的帶數據分析功能的防僞碼數量:\033[0m", 1, 0)
ffcode(incount, intype, "", schoice) # 調用ffcode()函數生成防僞碼
def scode5(schoice):
default_dir = r"codeauto.mri" # 設置默認打開的文件名
# 打開文件選擇對話框,指定打開的文件名稱爲“codeauto.aut”,擴展名爲“.mri”,記事本可以打開和編輯
file_path = tkinter.filedialog.askopenfilename(filetypes=[("Text file", "*.mri")], title=u"請選擇智能批處理文件:", initialdir=(os.path.expanduser(default_dir)))
codelist = openfile(file_path) # 讀取從文件選擇對話框中選取的文件
# 以換行符爲分割符讀取的信息內容轉換爲列表
codelist = codelist.split("\n")
print(codelist)
for item in codelist: # 讀取的信息循環生產防僞碼
codea = item.split(",")[0] # 信息用“,”分隔,逗號前面的信息存儲防僞碼標準信息
codeb = item.split(",")[1] # 信息用“,”分隔,逗號後面的信息存儲防僞碼標生產的數量
ffcode(codeb, codea, "no", schoice) # 調用ffcode函數批量生產同一標識信息的防僞碼
def scode6(schoice):
default_dir = r"D:\Python\codesystem\codepath\abcscode5.txt" # 設置默認打開的文件名稱
# 按默認的文件名稱打開文件選擇對話框,用於打開已經存在的防僞碼文件
file_path = tkinter.filedialog.askopenfilename(title=u"請選擇生成的防僞碼文件", initialdir=(os.path.expanduser(default_dir)))
codelist = openfile(file_path) # 讀取從文件選擇對話框中的文件
# 以換行符爲分界符將讀取的信息內容轉換爲列表
codelist = codelist.split("\n")
codelist.remove("") # 刪除列表中的空行
strset = codelist[0] # 讀取一行數據,一遍獲取原驗證碼的字母標誌信息
# 用maketrans()方法創建刪除數字的字符映射轉換表
remove_digits = strset.maketrans("", "", digits)
# 根據字符映射轉換表刪除該條碼中的數字,獲取字母標誌信息
res_letter = strset.translate(remove_digits)
nres_letter = list(res_letter) # 把信息列表變量nres_letter存儲
strpro = nres_letter[0] # 從列表中取得第一個字母,即區域分析碼
strtype = nres_letter[1] # 從列表中取得第二個字母,即色彩分析碼
strclass = nres_letter[2] # 從列表中取得第三個字母,即版次分析碼
# 去除信息中的括號和引號
nres_letter = strpro.replace(''''', '').replace(''''', '') + strtype.replace(
''''', '').replace(''''', '') + strclass.replace(''''', '').replace(''''', '')
card = set(codelist) # 將原有的防僞碼放到集合變量card中去
# 利用tkinter的messagebox提示用戶之前生產的防僞碼數量
tkinter.messagebox.showinfo("提示", "之前的防僞碼共計:" + str(len(card)))
root.withdraw() # 關閉提示窗口
incount = inputbox("請輸入補充防僞碼的數量:", 1, 0)
# 最大值按輸入生成數量的2倍生成新防僞碼
# 防止新生產防僞碼與原有防僞碼重複造成新生產的防僞碼數量不夠
for j in range(int(incount)*2):
randfir = random.sample(number, 3) # 隨機生成3位不重複的數字
randsec = sorted(randfir) # 對產生的數字進行排序
addcount = len(card) # 記錄集合中防僞碼的總數量
strone = "" # 清空集合中存儲防僞碼的變量
for i in range(9): # 生成9位的數字防僞碼
strone = strone + random.choice(number)
# 將三個數字按randsec變量中存儲的位置值添加到數字防僞碼中,並放到sim變量中
sim = str(strone[0:int(randsec[0])]) + strpro + str(
strone[int(randsec[0]):int(randsec[1])]) + strtype+ str(
strone[int(randsec[1]):int(randsec[2])]) + strclass + str(strone[int(randsec[2]):9]) + "\n"
card.add(sim) # 添加新生產的防僞碼到集合
# 如果添加到集合,證明生成的防僞碼與原有的防僞碼沒有產生重複
if len(card) > addcount:
randstr.append(sim) # 添加新生產的防僞碼到新的防僞碼列表
addcount = len(card) # 記錄新生產防僞碼集合的防僞碼數量
if len(randstr) >= int(incount): # 如果新生產的防僞碼數量達到輸入的防僞碼數量
print(len(randstr)) # 輸出已經生成防僞碼的數量
break # 退出循環
# 調用wfile函數,將生產的防僞碼屏幕輸出和文件輸出
wfile(randstr, nres_letter + "ncode" + str(choice) + ".txt", nres_letter, "生成後補防僞碼數量爲:", "codeadd")
# EAN-13條形碼批量生成
def scode7(schoice):
mainid = inputbox("\033[1;32m 請輸入EAN13的國家代碼(3位):\033[0m", 1, 0)
while int(mainid) <1 or len(mainid) != 3: # 驗證輸入是否爲3位數字
mainid = inputbox("\033[1;32m 請輸入EAN13的國家代碼(3位):\033[0m", 1, 0)
compid = inputbox("\033[1;32m 請輸入企業代碼(4位):\033[0m", 1, 0)
while int(compid ) < 1 or len(compid ) != 4: # 驗證輸入是否爲4位數字
compid = inputbox("\033[1;32m 請輸入企業代碼(4位):\033[0m", 1, 0)
incount = inputbox("\033[1;32m 請輸入要生成的條形碼數量:\033[0m", 1, 0)
while int(incount) == 0:
incount = inputbox("\033[1;32m 請輸入要生成的條形碼數量:\033[0m", 1, 0)
mkdir("barcode") # 判斷保存條形碼的文件夾是否存在,不存在,則創建該文件夾
for j in range(int(incount)): # 批量生產條形碼
strone = "" # 清空存儲單條條形碼的變量
for i in range(5): # 生成條形碼的五位企業商品代碼(數字)
strone = strone + str(random.choice(number))
barcode = mainid + compid + strone # 將國家代碼、企業代碼 、企業商品代碼進行組合
# 計算條形碼的效驗碼
evensum = int(barcode[1]) + int(barcode[3])+ int(barcode[5]) + int(barcode[7]) + int(barcode[9]) + int(barcode[11])
oddsum = int(barcode[0]) + int(barcode[2]) + int(barcode[4]) + int(barcode[6]) + int(barcode[8]) + int(barcode[10])
checkbit = (evensum*3 + oddsum) % 10
checkbit = int((10-checkbit)%10)
barcode = barcode + str(checkbit) # 組合成完整的EAN13條形碼
encode = EAN13Encoder(barcode) # 調用EAN13Encoder函數生成條形碼
encode.save("barcode\\" + barcode + ".png") # 保存條形碼信息圖片到文件夾
def scode8(schoice):
# 輸入要生成的二維碼數量
incount = inputbox("\033[1;32m 請輸入要生成的12位數字二維碼數量:\033[0m", 1, 0)
while int(incount) == 0:
incount = inputbox("\033[1;32m 請輸入要生成的12位數字二維碼數量:\033[0m", 1, 0)
mkdir("qrcode")
for j in range(int(incount)):
strone = "" # 清空存儲單條二維碼的變量
for i in range(12): # 生成單條二維碼數字
strone = strone + str(random.choice(number))
encoder = qrcode.make(strone) # 生成二維碼
encoder.save("qrcode\\" + strone + ".png") # 保存二維碼圖片到文件
def scode9(schoice):
default_dir = r"lottery.ini" # 默認打開文件項目路徑下的“lottery.ini”
# 選擇包含用戶抽獎信息票號的文件,擴展名爲“.ini”
file_path = tkinter.filedialog.askopenfilename(filetypes=[("Ini file", "*.ini")],
title=u"請選擇包含抽獎號碼的文件:", initialdir=(os.path.expanduser(default_dir)))
codelist = openfile(file_path) # 調用openfile()函數讀取剛打開的抽獎文件
codelist = codelist.split("\n") # 通過換行符吧抽籤信息分割爲抽獎列表
# 要求用戶輸入中(抽)獎數量
incount = inputbox("\033[1;32m 請輸入要生成的抽籤數量:\033[0m", 1, 0)
# 非法輸入,要求重新輸入
while int(incount) == 0 or len(codelist) < int(incount):
incount = inputbox("\033[1;32m 請輸入要生成的抽籤數量:\033[0m", 1, 0)
strone = random.sample(codelist, int(incount)) # 根據輸入的中獎數進行抽獎
for i in range(int(incount)):
# 將抽獎列表中的中括號去掉
wdata = str(strone[i].replace('[', '')).replace(']', '')
# 將抽獎列表中的引號去掉
wdata = wdata.replace(''''', '').replace(''''', '')
# 輸出中獎信息
print(wdata)
def inputbox(showstr, showorder, length):
instr = input(showstr) # showstr爲提示用戶輸入文字
if len(instr) != 0:
# 3種驗證方式,1(數字,不限位數),2(字母)3(數字且有位數要求)
if showorder == 1: # 數字,不限位數,大於0的整數
if str.isdigit(instr): # 驗證是否爲數字
if instr == 0:
print("\033[1;31;40m 非法輸入,請重新輸入!!!\033[0m")
return 0
else:
return instr
else:
print("\033[1;31;40m 非法輸入,請重新輸入!!!\033[0m")
return 0
if showorder == 2: # 驗證方式2,要求字母格式且是指定字母
if str.isalpha(instr): # 判斷輸入是否爲字母
if len(instr) != length: # 判斷輸入位數
print("\033[1;31;40m必須輸入"+ str(length)+"個字母,請重新輸入!!!\033[0m")
return 0
else:
return instr
else:
print("\033[1;31;40m 非法輸入,請重新輸入!!!\033[0m")
return 0
if showorder ==3: # 要求數字格式且對輸入位數有要求
if str.isdigit(instr): # 判斷輸入是否爲數字
if len(instr) != length: # 判斷輸入位數
print("\033[1;31;40m必須輸入"+ str(length)+"個數字,請重新輸入!!!\033[0m")
return 0
else:
return instr
else:
print("\033[1;31;40m 非法輸入,請重新輸入!!!\033[0m")
return 0
else:
print("\033[1;31;40m 非法輸入,請重新輸入!!!\033[0m")
return 0
def openfile(filename): # 讀取文件內容函數
f = open(filename)
fllist = f.read()
f.close()
return fllist
def mkdir(path): # 創建文件夾函數
isexists = os.path.exists(path)
if not isexists:
os.mkdir(path) # 文件件不存在時創建文件夾
# 實現屏幕輸出和文件輸出編碼信息函數,# sstr參數爲輸出防僞碼數據, sfile爲輸出的文件名稱
# typeis設置輸出完成後是否通過信息框提示, smsg爲信息提示框的提示文字,datapath 保存防僞碼的文件夾
def wfile(sstr, sfile, typeis, smsg,datapath):
mkdir(datapath) # 調用該函數創建文件夾
datafile = datapath + "\\" + sfile # 設置保存防僞碼的文件(包含路徑)
file = open(datafile, 'w') # 打開保存防僞碼的文件,如果文件不存在,則創建該文件
wrlist = sstr # 將防僞碼信息賦值給wrlist
pdata = "" # 清空變量pdata,pdata存儲屏幕輸出的防僞碼信息
wdata = "" # 清空變量 wdata , wdata 存儲保存到文本文件的防僞碼信息
for i in range(len(wrlist)): # 按條循環讀取防僞碼數據
wdata = str(wrlist[i].replace('[', '')).replace(']', '') # 去掉字符的中括號
wdata = wdata.replace(''''','').replace(''''', '') # 去掉字符的引號
file.write(str(wdata)) # 寫入保存防僞碼的文件
pdata = pdata + wdata # 將單條防僞碼存儲到pdata 變量
file.close() # 關閉文件
print("\033[1;31m" + pdata + "\033[0m") # 屏幕輸出生成的防僞碼信息
if typeis != "no": # 是否顯示“輸出完成”的信息提示框。如果typeis的值爲“no”,不現顯示
# 顯示“輸出完成”的信息提示框。顯示信息包含方位信息碼的保存路徑
tkinter.messagebox.showinfo("提示", smsg + str(len(randstr)) + "\n 防僞碼文件存放位置:" + datafile)
root.withdraw() # 關閉輔助窗口
# 生成含有數據分析功能的防僞編碼函數,參數scount爲要生成的防僞碼數量,typestr爲數據分析字符
# 參數ismessage在輸出完成時是否顯示提示信息,爲“no” 時不顯示;參數schoice爲設置輸出的文件名稱
def ffcode(scount, typestr, ismessage, schoice):
randstr.clear() # 清空保存批量防僞碼信息的變量
# 按數量生成含有數據分析功能的防僞編碼
for j in range(int(scount)):
strpro = typestr[0].upper() # 取得3個字母的第1個字母並轉爲大寫,區域分析碼
strtype = typestr[1].upper() # 取得3個字母的第2個字母並轉爲大寫,顏色分析碼
strclass = typestr[2].upper() # 取得3個字母的第3個字母並轉爲大寫,版本分析碼
randfir = random.sample(number, 3) # 隨機抽取防僞碼中的3個位置,不分先後
randsec = sorted(randfir) # 對抽取的位置進行排序並賦值給randsec變量
letterone = "" # 清空存儲單條防僞碼的變量
for i in range(9): # 生成9位數字防僞碼
letterone = letterone + random.choice(number)
# 將三個字母按randsec變量中存儲的位置值添加到數字防僞碼中,並保存到sim變量中
sim = str(letterone[0:int(randsec[0])]) + strpro + str(
letterone[int(randsec[0]):int(randsec[1])]) + strtype + str(
letterone[int(randsec[1]):int(randsec[2])]) + strclass + str(
letterone[int(randsec[2]):9]) + "\n"
randstr.append(sim) # 將組合生產的新防僞碼添加到randstr變量中
# 調用wfile函數,實現生成的防僞碼屏幕輸出和文件輸出
wfile(randstr, typestr + "scode" + str(schoice) + ".txt", ismessage, "生成含義數據分析功能的防僞碼共計:", "codepath")
# 通過循環控制用戶對程序功能的選擇
while i < 9:
# 調入程序主界面菜單
mainmenu()
# 鍵盤輸入需要操作的選項
choice = input("\033[1;32m 請輸入您要操作的菜單選項:\33[0m")
if len(choice) != 0: # 輸入如果不爲空
choice = input_validation(choice) # 驗證輸入是否爲數字
if choice == "1":
scode1( str(choice)) # 如果輸入大於零的整數,調用scode1()函數生成註冊碼
# 選擇菜單2,調用scode2()函數生成9位系列產品數字防僞編碼
if choice == "2":
scode2(choice)
# 選擇菜單3,調用scode3()函數生成25位混合產品序列號
if choice == "3":
scode3(choice)
# 選擇菜單4,調用scode4()函數生成含數據分析功能的防僞編碼
if choice == "4":
scode4(choice)
# 選擇菜單5,調用scode5()函數智能批量生成帶數據分析功能的防僞碼
if choice == "5":
scode5(choice)
# 選擇菜單6,調用scode6()函數後續補加生成防僞碼
if choice == "6":
scode6(choice)
# 選擇菜單7,調用scode7()函數批量生成條形碼
if choice == "7":
scode7( choice)
# 選擇菜單8,調用scode8()函數批量生成二維碼
if choice == "8":
scode8( choice)
# 選擇菜單9,調用scode9()函數生成企業粉絲抽獎程序
if choice == "9":
scode9( choice)
# 選擇菜單0,退出系統
if choice == "0":
i = 0
print("正在退出系統!!")
break
else:
print("\033[1;31;40m 輸入非法,請重新輸入!!\033[0m")
time.sleep(2)