Python中實(shí)現(xiàn) xls 文件轉(zhuǎn) xlsx的4種方法(示例詳解)
在 Python 中,可以采用 pandas、pyexcel、win32com 和 xls2xlsx 這四個(gè)模塊,實(shí)現(xiàn) xls 轉(zhuǎn) xlsx 格式。
以 Excel 示例文件 test_Excel.xls 為例,具體內(nèi)容如下圖所示:

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 系統(tǒng)下,但輸出的 xlsx 文件可以同時(shí)保留文本和格式。
3.xls2xlsx
安裝命令
pip install xlrd xls2xlsx -i https://mirrors.aliyun.com/pypi/simple
具體使用方法
from xls2xlsx import XLS2XLSX #學(xué)習(xí)中遇到問題沒人解答?小編創(chuàng)建了一個(gè)Python學(xué)習(xí)交流群: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 文件可以同時(shí)保留文本和格式信息,并且不依賴于 Windows 系統(tǒng)和 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 文件同樣只保留了文本,沒有保留格式信息。
到此這篇關(guān)于Python中實(shí)現(xiàn) xls 文件轉(zhuǎn) xlsx的4種方法的文章就介紹到這了,更多相關(guān)Python xls 文件轉(zhuǎn) xlsx內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python 窗體(tkinter)下拉列表框(Combobox)實(shí)例
這篇文章主要介紹了Python 窗體(tkinter)下拉列表框(Combobox)實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
python virtualenv虛擬環(huán)境配置與使用教程詳解
這篇文章主要介紹了python virtualenv虛擬環(huán)境配置與使用教程詳解,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07
python使用post提交數(shù)據(jù)到遠(yuǎn)程url的方法
這篇文章主要介紹了python使用post提交數(shù)據(jù)到遠(yuǎn)程url的方法,涉及Python使用post傳遞數(shù)據(jù)的相關(guān)技巧,需要的朋友可以參考下2015-04-04
Pandas使用query()優(yōu)雅的查詢實(shí)例
本文主要介紹了Pandas使用query()優(yōu)雅的查詢實(shí)例,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
詳解用Python調(diào)用百度地圖正/逆地理編碼API
這篇文章主要介紹了詳解用Python調(diào)用百度地圖正/逆地理編碼API,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
python實(shí)現(xiàn)簡易數(shù)碼時(shí)鐘
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)簡易數(shù)碼時(shí)鐘,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11

