Files
appointment_tool/src/utils/Passport_MRZutile.py
T
2023-02-20 17:28:40 +01:00

64 lines
1.6 KiB
Python

import re
from mrz.generator.td3 import TD3CodeGenerator
def encode(s: str) -> str:
s = s.encode(encoding='gb2312').hex()
res = []
for c in s:
res.append(chr(ord('a') + int(c, base=16)))
return "".join(res).upper()
def decode(s: str) -> str:
t = []
for c in s:
t.append(format((ord(c) - ord('A')), 'x'))
t = re.findall('.{1,2}', "".join(t))
res = []
for c in t:
res.append(int("0x" + c, 16))
return bytes(res).decode('gb2312')
chinese_name = "黄晴风"
encoded_chinese_name = encode(chinese_name)
print(encoded_chinese_name)
optional_data_length = 14 - len(encoded_chinese_name)
for i in range(0, optional_data_length):
encoded_chinese_name = encoded_chinese_name + "<"
first_name = "Qingfeng"
last_name = "Huang"
passport_number = "991011946"
birth_day = "980118"
sex = "F"
# sex = "M"
# optinal_data = "MFMLMANK<<<<A9" #14位
# nationality = "CHN"
nationality = "CHN"
if nationality == "TWN":
optinal_data = "I100217437" # 14位
document_prefix = "P"
else:
optinal_data = encoded_chinese_name # 14位
document_prefix = "PO"
expire_date = "270116"
# first_name = "Lei"
# last_name = "PAN"
# passport_number = "G22611379"
# birth_day = "841215"
# sex = "M"
# optinal_data = "19203301<<<<<<" #14位
# expire_date = "170510"
code = TD3CodeGenerator(document_prefix, nationality, last_name, first_name, passport_number, nationality, birth_day,
sex, expire_date,
optinal_data)
# code = TD3CodeGenerator("PO", "CHN", "PAN", "LEI", "E90600575", "CHN", "841215", "M", "270116", "MFMLMANK<<<<A9")
print(code)