DeepSeek辅助编写的将ET格式文件转换为xls和xlsb格式程序
1.ET格式文件转换为xls
import xlrd
import xlwtdef et_to_xls(input_et, output_xls):"""将ET文件转换为XLS格式参数:input_et: 输入的ET文件路径output_xls: 输出的XLS文件路径"""try:# 1. 使用xlrd读取ET文件print(f"正在读取ET文件: {input_et}")et_book = xlrd.open_workbook(input_et)# 2. 创建xls工作簿xls_book = xlwt.Workbook(encoding='utf-8')# 3. 复制每个工作表print("开始转换工作表...")for sheet_index in range(et_book.nsheets):et_sheet = et_book.sheet_by_index(sheet_index)# 创建对应的xls工作表xls_sheet = xls_book.add_sheet(et_sheet.name)print(f" 转换工作表: {et_sheet.name} (共{et_sheet.nrows}行)")style_normal = xlwt.easyxf()style_date=xlwt.easyxf(num_format_str='YYYY-MM-DD')style_int=xlwt.easyxf(num_format_str='0')style_double=xlwt.easyxf(num_format_str='0.00') #defined outside of the loop to solve error of More than 4094 XFs (styles)# 4. 复制单元格数据和格式for row in range(et_sheet.nrows):for col in range(et_sheet.ncols):try:cell = et_sheet.cell(row, col)value = cell.value# 处理不同数据类型if cell.ctype == xlrd.XL_CELL_DATE:# 转换日期格式value = xlrd.xldate.xldate_as_datetime(value, et_book.datemode)style = style_dateelif cell.ctype == xlrd.XL_CELL_NUMBER:style =style_int if value==int(value) else style_doubleelse:style = style_normalxls_sheet.write(row, col, value, style)except Exception as cell_error:print(f" 警告: 处理单元格({row+1},{col+1})时出错: {str(cell_error)}")continue# 5. 保存xls文件print(f"正在保存XLS文件: {output_xls}")xls_book.save(output_xls)print("转换完成!")except xlrd.XLRDError as e:print(f"错误: 无法读取ET文件 - {str(e)}")except Exception as e:print(f"发生未知错误: {str(e)}")# 示例用法
if __name__ == "__main__":input_file = "ducknyc200.et" # 替换为你的ET文件路径output_file = "Output.xls" # 输出文件路径et_to_xls(input_file, output_file)# 验证转换结果try:print("\n验证转换结果:")xls_book = xlrd.open_workbook(output_file)print(f"成功读取输出文件,包含 {xls_book.nsheets} 个工作表")for sheet in xls_book.sheets():print(f" - {sheet.name}: {sheet.nrows}行 x {sheet.ncols}列")except Exception as e:print(f"验证失败: {str(e)}")
执行输出
python et2xls.py
正在读取ET文件: ducknyc200.et
开始转换工作表...转换工作表: ducknyc200 (共201行)
正在保存XLS文件: Output.xls
转换完成!验证转换结果:
成功读取输出文件,包含 1 个工作表- ducknyc200: 201行 x 41列
2.ET格式文件转换为xlsb
import xlrd
from pyxlsbwriter import XlsbWriter
from datetime import datetimedef et_to_xlsb(input_et, output_xlsb):"""将ET文件转换为XLSB格式参数:input_et: 输入的ET文件路径output_xlsb: 输出的XLSB文件路径"""try:# 1. 使用xlrd读取ET文件print(f"正在读取ET文件: {input_et}")et_book = xlrd.open_workbook(input_et)print(et_book.nsheets);# 2. 创建XLSB工作簿print(f"正在创建XLSB文件: {output_xlsb}")with XlsbWriter(output_xlsb) as xlsb_writer:# 添加工作表xlsb_writer.add_sheet("Sheet1")# 3. 处理每个工作表for sheet_index in range(et_book.nsheets):et_sheet = et_book.sheet_by_index(sheet_index)print("et_sheet.ncols", et_sheet.ncols);print(f" 处理工作表: {et_sheet.name} (共{et_sheet.nrows}行)")# 写入数据data_to_write = []# 添加标题行(如果有特殊处理需求可以在此修改)if et_sheet.nrows > 0:first_row = [et_sheet.cell_value(0, col) for col in range(et_sheet.ncols)]#print(first_row)data_to_write.append(first_row)# 添加数据行for row in range(1 if et_sheet.nrows > 0 else 0, et_sheet.nrows):row_data = []for col in range(et_sheet.ncols):cell = et_sheet.cell(row, col)#处理不同数据类型if cell.ctype == xlrd.XL_CELL_DATE:#转换日期格式dt = xlrd.xldate.xldate_as_datetime(cell.value, et_book.datemode)row_data.append(dt)elif cell.ctype == xlrd.XL_CELL_NUMBER:row_data.append(float(cell.value))elif cell.ctype == xlrd.XL_CELL_BOOLEAN:row_data.append(bool(cell.value))else:row_data.append(str(cell.value)[:200]) # use [:200] to solve 'B' format requires 0 <= number <= 255data_to_write.append(row_data)#print(data_to_write[2])# 写入XLSB文件xlsb_writer.write_sheet(data_to_write)print("转换完成!")except xlrd.XLRDError as e:print(f"错误: 无法读取ET文件 - {str(e)}")except Exception as e:print(f"发生未知错误: {str(e)}")if __name__ == "__main__":# 配置输入输出input_file = "ducknyc200.et" # 替换为你的ET文件路径output_file = "ducknyc200.xlsb" # 输出文件路径et_to_xlsb(input_file, output_file)
执行输出
python et2xlsb.py
正在读取ET文件: ducknyc200.et
1
正在创建XLSB文件: ducknyc200.xlsb
et_sheet.ncols 41处理工作表: ducknyc200 (共201行)
转换完成!
转换前后的文件大小
2025/08/18 18:36 170,496 ducknyc200.et
2025/08/18 19:53 137,728 output.xls
2025/08/18 20:31 43,152 ducknyc200.xlsb