Python中4种方法实现 xls 文件转 xlsx

在 Python 中,可以采用 pandas、pyexcel、win32com 和 xls2xlsx 这四个模块,实现 xls 转 xlsx 格式。

以 Excel 示例文件 test_Excel.xls 为例,具体内容如下图所示:
在这里插入图片描述

1.pandas

安装命令

pip install pandas -i https://mirrors.aliyun.com/pypi/simple

具体使用方法

import pandas as pd

filename = "test_Excel.xls"
outfile = "test_Excel-pandas.xlsx"
# Read Excel xls file
data = pd.read_excel(filename)
# Write to xlsx file with no row index
data.to_excel(outfile, index=False)

注:上面的方法输出的 xlsx 文件同样只保留了文本,没有保留格式信息。

2.win32com

安装命令

python -m pip install pywin32 -i https://mirrors.aliyun.com/pypi/simple

具体使用方法

import os
import win32com.client as win32

filename = "test_Excel.xls"
outfile = "test_Excel-win32.xlsx"
# Open up Excel 
excel = win32.gencache.EnsureDispatch("Excel.Application")
# Open xls file
wb = excel.Workbooks.Open(os.path.abspath(filename))
# Save as xlsx file
wb.SaveAs(os.path.abspath(outfile), FileFormat=51)
wb.Close()
excel.Application.Quit()

注:win32com 模块只适用于已安装 Excel 软件的Windows 系统下,但输出的 xlsx 文件可以同时保留文本和格式。

3.xls2xlsx

安装命令

pip install xlrd xls2xlsx -i https://mirrors.aliyun.com/pypi/simple

具体使用方法

from xls2xlsx import XLS2XLSX
#学习中遇到问题没人解答?小编创建了一个Python学习交流群:725638078
filename = "test_Excel.xls"
outfile = "test_Excel-x2x.xlsx"
# Read xls file
x2x = XLS2XLSX(filename)
# Write to xlsx file
x2x.to_xlsx(outfile)

注:使用上面的方法得到的 xlsx 文件可以同时保留文本和格式信息,并且不依赖于 Windows 系统和 Excel 程序。

4.pyexcel

安装命令

pip install pyexcel -i https://mirrors.aliyun.com/pypi/simple

具体使用方法

import pyexcel

filename = "test_Excel.xls"
outfile = "test_Excel-pyexcel.xlsx"
# Convert xls file to xlsx directly
pyexcel.save_book_as(file_name=filename, dest_file_name=outfile)

注:上面的方法输出的 xlsx 文件同样只保留了文本,没有保留格式信息。

  • 6
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 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
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值