Python xls文件和xlsx文件格式互相转换

Python xls文件和xlsx文件格式互相转换

1. xls文件另存为xlsx
import win32com.client
xls_path = 'D:\\1_Work_Task\\01_Integrate_test\\2_IFC2.0_integrate_test\\test_report\\report.xls'
# 格式转换
e = win32com.client.Dispatch('Excel.application')
pro = e.Workbooks.Open(xls_path)  # 打开要转换的excel
# 另存为新文件
file_Name = os.path.splitext(xls_path)
new_excel_path = file_Name[0] + "_new.xlsx"
pro.SaveAs(new_excel_path, FileFormat=51)  # 另存为xls格式
pro.Close()
e.Application.Quit()
2. xlsx文件另存为xls
import win32com.client
xls_path = 'D:\\1_Work_Task\\01_Integrate_test\\2_IFC2.0_integrate_test\\test_report\\report.xlsx'
# 格式转换
e = win32com.client.Dispatch('Excel.application')
pro = e.Workbooks.Open(xls_path)  # 打开要转换的excel
# 另存为新文件
file_Name = os.path.splitext(xls_path)
new_excel_path = file_Name[0] + "_new.xls"
pro.SaveAs(new_excel_path, FileFormat=56)  # 另存为xlsx格式
pro.Close()
e.Application.Quit()
  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
你可以使用openpyxl库将xls文件转换xlsx文件。下面是一个示例代码: ```python import openpyxl from openpyxl.utils import get_column_letter from openpyxl import Workbook from openpyxl.compat import range def convert_xls_to_xlsx(xls_file, xlsx_file): # 打开xls文件 xls_workbook = openpyxl.load_workbook(xls_file, read_only=True, keep_vba=False, data_only=True) # 创建新的xlsx文件 xlsx_workbook = Workbook() # 遍历xls文件的每个工作表 for sheet_name in xls_workbook.sheetnames: xls_sheet = xls_workbook[sheet_name] xlsx_sheet = xlsx_workbook.create_sheet(title=sheet_name) # 复制每个单元格的值和样式到新的xlsx文件 for row in xls_sheet.iter_rows(): for cell in row: xlsx_sheet[cell.coordinate].value = cell.value if cell.has_style: xlsx_sheet[cell.coordinate].font = cell.font xlsx_sheet[cell.coordinate].border = cell.border xlsx_sheet[cell.coordinate].fill = cell.fill xlsx_sheet[cell.coordinate].number_format = cell.number_format xlsx_sheet[cell.coordinate].alignment = cell.alignment # 调整列宽 for column_cells in xls_sheet.columns: max_length = 0 column = column_cells[0].column_letter for cell in column_cells: try: if len(str(cell.value)) > max_length: max_length = len(cell.value) except: pass adjusted_width = (max_length + 2) * 1.2 xlsx_sheet.column_dimensions[column].width = adjusted_width # 删除默认创建的空白工作表 del xlsx_workbook["Sheet"] # 保存xlsx文件 xlsx_workbook.save(xlsx_file) # 示例用法 xls_file = "example.xls" xlsx_file = "example.xlsx" convert_xls_to_xlsx(xls_file, xlsx_file) ``` 在示例,我们首先使用`openpyxl.load_workbook()`函数打开xls文件,然后使用`openpyxl.Workbook()`创建一个新的xlsx文件。接下来,我们遍历xls文件的每个工作表,并将每个单元格的值和样式复制到新的xlsx文件。最后,我们调整每列的宽度,并删除默认创建的空白工作表。最后,我们使用`xlsx_workbook.save()`函数保存xlsx文件。 确保在运行代码之前安装openpyxl库,可以使用以下命令进行安装: ``` pip install openpyxl ``` 请将`xls_file`和`xlsx_file`变量替换为你实际的文件路径。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_长风_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值