繁体中文 8300
utf-8 4110
参考
https://wiki.scn.sap.com/wiki/display/ABAP/Character+encoding+conversion
def txt_to_xlsx(txt_path):
line_list = []
with open(txt_path, 'r', encoding='windows-1252') as f:
lines = f.readlines()
for line in lines:
sep = re.findall(r'\|', line)
if len(sep) > 3:
line= line.lstrip('|')
# print(line)
line_list.append(line)
with open(txt_path, "w", encoding='windows-1252') as f:
f.writelines(line_list)
xlsx_path = txt_path.replace('.txt', '.xlsx')
# if os.path.exists(xlsx_path):
# return
# if not txt_path.endswith('.txt'):
# return
xlApp = win32com.client.DispatchEx("Excel.Application")
xlApp.Visible = True
xlApp.DisplayAlerts = 0
# print(txt_path)
wb = xlApp.Workbooks.OpenText(Filename=txt_path,
Origin=950, # 指定的编码,繁体中文是950
StartRow=1,
DataType=1,
TextQualifier=1,
ConsecutiveDelimiter=False,
Tab=True,
Semicolon=False,
Comma=False,
Space=False,
Other=True,
OtherChar="|",
FieldInfo=[1,1],
TrailingMinusNumbers=True,
)
# wb.Close(True)
xlApp.ActiveWorkbook.SaveAs(Filename=xlsx_path, FileFormat=51)
# wb.close()
xlApp.Quit()