python实现企业编码生成系统

外部模块:

> pip install qrcode
> pip install pystrich

代码如下:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import os, tkinter, random, string, time, qrcode
from pystrich.ean13 import EAN13Encoder
import tkinter.messagebox
import tkinter.filedialog
from tkinter import *
from string import digits

root = tkinter.Tk()  # tkinter模块为python的标准图形界面接口。本代码的目的是建立根窗口

# 初始化数据
number = "1234567890"
letter = "ABCDEFGHIGKLMNOPQRSTUVWXYZ1234567890"
allis = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!@#$%^&*()_+"
i = 0
randstr = []
fourth = []
fifth = []
randfir = ""
randsec = ""
randthr = ""
str_one = ""
strone = ""
strtwo = ""
nextcard = ""
userput = ""
nres_letter = ""


# 验证输入是否非法
def input_validation(insel):
    if str.isdigit(insel):
        # if insel == 0:
        #     print("\033[1;31;40m    输入非法,请重新输入!!!\033[0m")
        #     return 0
        # else:
        #     return insel
        insel = int(insel)
        return insel
    else:
        print("\033[1;31;40m    输入非法,请重新输入!!!\033[0m")
        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


# 输入验证判断函数,根据参数判断输入的是哪种类型,是否合法,
# showstr为input函数提供动态输入提示文字,
# showorder提供验证方式,length提供要求输入数据的长度
def inputbox(showstr, showorder, length):
    instr = input(showstr)  # 使用input函数要求用户输入信息,showstr为输入提示文字
    if len(instr) != 0:
        # 根据输入数据的要求,分成三种验证方式验证,1:数字,不限位数;2:字母;3:数字且有位数要求
        if showorder == 1:
            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
        if showorder == 2:# 验证方式2 ,要求字母格式,且是三个字母
            if str.isalpha(instr):
                if len(instr) != length:# 判断输入的是否为三个字母,如果不是,则要求重新输入,返回值为“0”
                    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: # 验证方式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"


# 编码输出显示函数,通过屏幕输出和文件输出两种方式输出生成的防伪码信息
# 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
    file.close()
    print("\033[1;31m" + pdata + "\033[0m")
    if typeis != "no":
        # 显示“输出完成”的信息提示框。显示信息包含方位信息码的保存路径
        tkinter.messagebox.showinfo("提示", smsg + str(len(randstr)) + "\n 防伪码文件存放位置:" + datafile)
        root.withdraw()  # 关闭辅助窗口


# 生成6位数字防伪码
def scode1(schoice):
    incount = inputbox("\033[1;32m    请输入您要生成防伪码的数量:\33[0m", 1, 0)
    while int(incount) == 0:
        incount = inputbox("\033[1;32m    请输入您要生成防伪码的数量:\33[0m", 1, 0)
    randstr.clear()
    for j in range(int(incount)):
        randfir = ''
        for i in range(6):
            randfir = randfir + random.choice(number)
        randfir = randfir + "\n"
        randstr.append(randfir)
    wfile(randstr, "scode" + str(schoice) + ".txt", "", "已生成6位防伪码共计:", "codepath")


# 生成9位系列产品数字防伪编码
def scode2(schoice):
    ordstart = inputbox("\033[1;32m    请输入系列产品的数字起始号(3位):\33[0m", 3, 3)
    while int(ordstart) == 0:
        ordstart = inputbox("\033[1;32m    请输入系列产品的数字起始号(3位):\33[0m", 1, 0)
    ordcount = inputbox("\033[1;32m    请输入产品系列的数量:", 1, 0)
    while int(ordcount) < 1 or int(ordcount) > 9999:
        ordcount = inputbox("\033[1;32m    请输入产品系列的数量:", 1, 0)
    incount = inputbox("\033[1;32m    请输入您要生成的每个系列产品的防伪码的数量:", 1, 0)
    while int(incount) == 0:
        incount = inputbox("\033[1;32m    请输入您要生成防伪码的数量:\\33[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(randstr, "scode" + str(schoice) + ".txt", "", "已生成9位系列产品防伪码共计:", "codepath")

# 生成25位混合产品序列号函数,参数schoice设置输出的文件名称
def scode3(schoice):
    incount = inputbox("\033[1;32m    请输入您要生成的25位混合产品序列号数量:\33[0m", 1, 0)
    while int(incount) == 0:
        incount = inputbox("\033[1;32m    请输入您要生成的25位混合产品序列号数量:\33[0m", 1, 0)
    randstr.clear()
    for j in range(int(incount)):
        strone = ''
        for i in range(25):
            strone = strone + random.choice(letter)
        strtwo = strone[:5] + "-" + strone[5:10] + "-" + strone[10:15] + "-" + strone[15:20] + "-" + strone[
                                                                                                     20:25] + "\n"
        randstr.append(strtwo)
    wfile(randstr, "scode" + str(schoice) + ".txt", "", "已生成25混合防伪序列码共计:", "codepath")

# 生成含数据分析功能防伪编码函数,参数schoice设置输出的文件名称
def scode4(schoice):
    intype = inputbox("\033[1;32m    请输入数据分析编号(3位字母):\33[0m", 2, 3)
    while not str.isalpha(intype) or len(intype) != 3:
        intype = inputbox("\033[1;32m    请输入数据分析编号(3位字母):\33[0m", 2, 3)
    incount = inputbox("\033[1;32m    输入您要生成的带数据分析功能的防伪码数量:\33[0m", 1, 0)
    while int(incount) == 0:
        incount = inputbox("\033[1;32m    输入您要生成的带数据分析功能的防伪码数量:\33[0m", 1, 0)
    ffcode(incount, intype, "", schoice)# 调用 ffcode函数生成注册信息

# 生成含数据分析功能防伪编码函数,参数scount为要生成的防伪码数量,typestr为数据分析字符
# 参数ismessage在输出完成时是否显示提示信息,为“no”不显示,为其他值显示;
# 参数schoice设置输出的文件名称
def ffcode(scount, typestr, ismessage, schoice):
    randstr.clear()
    for j in range(int(scount)):
        strpro = typestr[0].upper()
        strtype = typestr[1].upper()
        strclass = typestr[2].upper()
        randfir = random.sample(number, 3)
        randsec = sorted(randfir)
        letterone = ""
        for i in range(9):
            letterone = letterone + random.choice(number)
        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)
    wfile(randstr, typestr + "scode" + str(schoice) + ".txt", ismessage, "生成含数据分析功能的防伪码共计:", "codepath")

# 生成含数据分析功能防伪编码函数,参数schoice设置输出的文件名称
def scode5(schoice):
    default_dir = r"codeauto.mri"
    # 打开文件选择对话框,指定打开的文件名称为"mrsoft.mri" ,扩展名为“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)

#  后续补加生成防伪码函数,防伪码格式为带数据分析功能注册码
def scode6(schoice):
    default_dir = r""
    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]
    remove_digits = strset.maketrans("", "", digits)
    res_letter = strset.translate(remove_digits)
    nres_letter = list(res_letter)
    strpro = nres_letter[0]
    strtype = nres_letter[1]
    strclass = nres_letter[2]
    nres_letter = strpro.replace("'", '').replace('(', '').replace(')', '') + \
                  strtype.replace("'", '').replace('(', '').replace(')', '') + strclass.replace("'", '').replace('(',
                                                                                                                 '').replace(
        ')', '')
    print("sssssssssss", nres_letter)
    card = set(codelist)
    tkinter.messagebox.showinfo("提示", "之前的防伪码共计:" + str(len(card)))
    root.withdraw()
    incount = inputbox("请输入补充防伪码生成的数量:", 1, 0)
    for j in range(int(incount) * 2):
        randfir = random.sample(number, 3)
        randsec = sorted(randfir)
        addcount = len(card)
        strone = ""
        for i in range(9):
            strone = strone + random.choice(number)
        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(randstr, nres_letter + "ncode" + str(schoice) + ".txt", nres_letter, "生成后补防伪码共计:", "codeadd")

#  条形码EAN13批量生成函数
def scode7(schoice):
    mainid = inputbox("\033[1;32m    请输入EN13的国家代码(3位):\33[0m", 3, 3)
    while int(mainid) < 1 or len(mainid) != 3:
        mainid = inputbox("\033[1;32m    请输入EN13的国家代码(3位):\33[0m", 3, 3)
    compid = inputbox("\033[1;32m    请输入企业代码(4位):\33[0m", 3, 4)
    while int(compid) < 1 or len(compid) != 4:
        compid = inputbox("\033[1;32m    请输入企业代码(4位):\33[0m", 3, 4)
    incount = inputbox("\033[1;32m    请输入要生成的条形码数量:\33[0m", 1, 0)
    while int(incount) == 0:
        incount = inputbox("\033[1;32m    请输入要生成的条形码数量:\33[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 = int((10 - (evensum * 3 + oddsum) % 10) % 10)
        barcode = barcode + str(checkbit)
        print(barcode)
        encoder = EAN13Encoder(barcode)
        encoder.save("barcode\\" + barcode + ".png")

# 本函数生成固定的12位二维码,读者可以根据实际需要修改成按输入位数进行生成的函数
def scode8(schoice):
    incount = inputbox("\033[1;32m    请输入要生成的12位数字二维码数量:\33[0m", 1, 0)
    while int(incount) == 0:
        incount = inputbox("\033[1;32m    请输入要生成的12位数字二维码数量:\33[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"
    file_path = tkinter.filedialog.askopenfilename(filetypes=[("Ini file", "*.ini")], title=u"请选择包含抽奖号码的抽奖文件:",
                                                   initialdir=(os.path.expanduser(default_dir)))
    codelist = openfile(file_path)
    codelist = codelist.split("\n")
    incount = inputbox("\033[1;32m    请输入要生成的抽奖数量:\33[0m", 1, 0)
    while int(incount) == 0 or len(codelist) < int(incount):
        incount = inputbox("\033[1;32m    请输入要生成的抽奖数量:\33[0m", 1, 0)
    strone = random.sample(codelist, int(incount))
    print("\033[1;32m    抽奖信息名单发布:\33[0m")
    for i in range(int(incount)):
        wdata = str(strone[i].replace('[', '').replace(']', ''))
        wdata = wdata.replace("'", '').replace("'", '')
        print("\033[1;32m    " + wdata + "\33[0m")


# 主程序界面
def mainmenu():
    print("""\033[1;35m
    ***********************************************************
                        企业编码生成系统
    ***********************************************************
        1.生成6位数字防伪编码(213563型)
        2.生成9位系列产品数字防伪编码(879-335439型)
        3.生成25位混合产品序列号(B2R12-N7TE8-9IET2-FE35O-DW2K4型)
        4.生成含数据分析功能的防伪编码(5A61M0583D2)
        5.智能批量生成带数据分析功能的防伪码
        6.后续补加生成防伪码(5A61M0583D2)
        7.EAN-13条形码批量生成
        8.二维码批量输出
        9.企业粉丝防伪码抽奖
        0.退出系统
    ***********************************************************
    说明:通过数字键选择菜单
    ***********************************************************
    \033[0m""")


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))
        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 == 9:
            scode9(choice)
        if choice == 0:
            i = 0
            print("正在退出系统!!")
            break
    else:
        print("\033[1;31;40m    输入非法,请重新输入!!!\033[0m")
        time.sleep(2)

if __name__ == '__main__':
    mainmenu()

打包成可执行exe文件

>pyinstaller -F D:\PythonProject\02企业编码生成系统\EncodeMakeSys.py
  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python企业编码生成系统是基于Python编程语言开发的一个工具,旨在帮助企业快速、准确地生成各种编码。 首先,Python企业编码生成系统具有可定制性的特点。通过系统提供的各种配置,企业可以根据自身需求定制生成编码的规则,如编码长度、编码格式、编码前缀等。这样,企业可以根据自己的产品特点,生成符合自己需求的独特编码。 其次,Python企业编码生成系统具有高效率的优势。系统采用Python语言编写,具有快速的执行速度和高效的计算能力,可以在短时间内生成大量编码。这对于一些需要大规模编码生成企业来说,可以提高工作效率,节约时间成本。 此外,Python企业编码生成系统还具备易用性和灵活性。系统提供了简单直观的用户界面,用户可以通过简单的操作完成编码生成的配置和执行过程。同时,系统也提供了多种输出方式,如文本格式、Excel表格等,方便用户根据需要进行使用和保存。 最后,Python企业编码生成系统还具备良好的扩展性。由于Python语言的开源性和强大的生态圈,用户可以自定义开发插件和扩展功能,满足不同业务的需求。这样,用户可以根据自己的具体情况扩展系统的功能,实现更多个性化的编码生成需求。 综上所述,Python企业编码生成系统是一个灵活、高效、可定制化的工具,可以帮助企业快速生成各种编码,提高工作效率,减少人力成本,并满足不同企业的个性化需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值