新改进功能:循环输入数据并按照一定规则存储。
import sys
import openpyxl
wb = openpyxl.Workbook()
wa = wb.active
wa.column_dimensions['B'].width = 15
wa.column_dimensions['C'].width = 20
wa.column_dimensions['D'].width = 30
wa.append(['ID','SerialNumber','BarCode','EPC'])
Header_bits = '00110000' #8bits
#Filter_bits = '011' #3过滤值 3bits
Filter_bits = '001' #1过滤值 3bits
BcNumArr = [] #条码与数量的二维数组
ExcelIndex = 1 #excel插入行数据索引
# base = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F]
base = [str(x) for x in range(10)] + [ chr(x) for x in range(ord('A'),ord('A')+6)]
ID = int(input('ID:')) - 1
StId = ID + 1
GS1_Company_Prefix_Length = int(input('GS1 Company Prefix Length(6-12):'))
Serial_Number = int(input('SerialNumber:')) - 1 #序列号
Start_Serial_Number = Serial_Number #存储起
#循环输入条码与想对应生成的数量
Bol_Inp = 'y'
while Bol_Inp == 'y':
TempArr = []
BarCode = input('BarCode:')
ALL_Number = int(input('ALL_Number:')) #生成数量
TempArr.append(BarCode)
TempArr.append(ALL_Number)
BcNumArr.append(TempArr)
Bol_Inp = input('是否继续输入(y/n):')
#判断输入的BarCode与Serial number是否符合规则
if(BarCode.__len__()!=14):
print('BarCode必须是14位')
sys.exit()
elif not BarCode.isdigit():
print('BarCode的所有字符必须是数字')
sys.exit()
if(Serial_Number + 1<0):
print('Serial_Number必须大于或等于0')
sys.exit()
#根据GS1公司前缀长度进而判断
if GS1_Company_Prefix_Length == 6:
Cutoff_Point = 7
GS1_Company_Prefix_bitslen = 20
ID_IRN_bitslen = 24
Partition_bits = '110' # 6
elif GS1_Company_Prefix_Length == 7:
Cutoff_Point = 8
GS1_Company_Prefix_bitslen = 24
ID_IRN_bitslen = 20
Partition_bits = '101' # 5
elif GS1_Company_Prefix_Length == 8:
Cutoff_Point = 9
GS1_Company_Prefix_bitslen = 27
ID_IRN_bitslen = 17
Partition_bits = '100' # 4
elif GS1_Company_Prefix_Length == 9:
Cutoff_Point = 10
GS1_Company_Prefix_bitslen = 30
ID_IRN_bitslen = 14
Partition_bits = '011' # 3
elif GS1_Company_Prefix_Length == 10:
Cutoff_Point = 11
GS1_Company_Prefix_bitslen = 34
ID_IRN_bitslen = 10
Partition_bits = '010' # 2
elif GS1_Company_Prefix_Length == 11:
Cutoff_Point = 12
GS1_Company_Prefix_bitslen = 37
ID_IRN_bitslen = 7
Partition_bits = '001' # 1
elif GS1_Company_Prefix_Length == 12:
Cutoff_Point = 13
GS1_Company_Prefix_bitslen = 40
ID_IRN_bitslen = 4
Partition_bits = '000' # 0
else:
print('公司前缀长度必须在6-12之间')
sys.exit()
# bin2dec
# 二进制 to 十进制: int(str,n=10)
def bin2dec(string_num):
return str(int(string_num, 2))
# dec2bin
# 十进制 to 二进制: bin()
def dec2bin(string_num):
num = int(string_num)
mid = []
while True:
if num == 0: break
num,rem = divmod(num, 2)
mid.append(base[rem])
return ''.join([str(x) for x in mid[::-1]])
# dec2hex
# 十进制 to 十六进制: hex()
def dec2hex(string_num):
num = int(string_num)
mid = []
while True:
if num == 0: break
num,rem = divmod(num, 16)
mid.append(base[rem])
return ''.join([str(x) for x in mid[::-1]])
#二进制 to 十六进制
def bin2hex(string_num):
return dec2hex(bin2dec(string_num))
# hex2dec
# 十六进制 to 十进制
def hex2dec(string_num):
return str(int(string_num.upper(), 16))
# 十六进制 to 二进制: bin(int(str,16))
def hex2bin(string_num):
return dec2bin(hex2dec(string_num.upper()))
#为字符串按照输入长度补'0'
def add_zero(str,gd_length):
if (str.__len__() < gd_length):
add_zero = '0'
for i in range(gd_length - str.__len__() - 1):
add_zero += '0'
str = add_zero + str
return str
'''
#Serial_Number化为二进制,如果不够38位,前面加'0'
Serial_Number_bits = add_zero(dec2bin(Serial_Number),38)
all_bits = Header_bits + Filter_bits + Partition_bits + GS1_Company_Prefix_bits + ID_IRN_bits + Serial_Number_bits
Hexadecimal = bin2hex(all_bits)
print('EPC:')
print(Hexadecimal)
'''
GTIN_BITS_PRO = Header_bits + Filter_bits + Partition_bits
#输出指定条数的EPC码并保存到excel
for i in range(BcNumArr.__len__()):
GS1_Company_Prefix = BcNumArr[i][0][1:Cutoff_Point]
ID_IRN = BarCode[0] + BcNumArr[i][0][Cutoff_Point:13]
# GS1_Company_Prefix化为二进制,如果不够规定位数,前面加'0'
GS1_Company_Prefix_bits = add_zero(dec2bin(GS1_Company_Prefix), GS1_Company_Prefix_bitslen) # 24bits
# ID_IRN 化为二进制,如果不够规定位数,前面加'0'
ID_IRN_bits = add_zero(dec2bin(ID_IRN), ID_IRN_bitslen)
GTIN_BITS = GTIN_BITS_PRO + GS1_Company_Prefix_bits + ID_IRN_bits
for j in range(BcNumArr[i][1]):
ExcelIndex += 1
ID += 1
Serial_Number += 1
wa.cell(ExcelIndex, 1, str(ID))
wa.cell(ExcelIndex, 2, str(Serial_Number))
wa.cell(ExcelIndex, 3, BcNumArr[i][0])
wa.cell(ExcelIndex, 4, bin2hex(GTIN_BITS + add_zero(dec2bin(Serial_Number),38)))
Serial_Number = Start_Serial_Number
wb.save(str(StId) + '-' + str(ID) + '.xlsx')