Python中實現(xiàn) xls 文件轉(zhuǎn) xlsx的4種方法(示例詳解)
在 Python 中,可以采用 pandas、pyexcel、win32com 和 xls2xlsx 這四個模塊,實現(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 文件可以同時保留文本和格式。
3.xls2xlsx
安裝命令
pip install xlrd xls2xlsx -i https://mirrors.aliyun.com/pypi/simple
具體使用方法
from xls2xlsx import XLS2XLSX #學(xué)習(xí)中遇到問題沒人解答?小編創(chuàng)建了一個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 文件可以同時保留文本和格式信息,并且不依賴于 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中實現(xiàn) xls 文件轉(zhuǎn) xlsx的4種方法的文章就介紹到這了,更多相關(guān)Python xls 文件轉(zhuǎn) xlsx內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python 安裝教程之Pycharm安裝及配置字體主題,換行,自動更新
這篇文章主要介紹了python 安裝教程之Pycharm安裝及配置字體主題,換行,自動更新,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03使用tensorflow 實現(xiàn)反向傳播求導(dǎo)
這篇文章主要介紹了使用tensorflow 實現(xiàn)反向傳播求導(dǎo),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-05-05pyqt5對用qt designer設(shè)計的窗體實現(xiàn)彈出子窗口的示例
今天小編就為大家分享一篇pyqt5對用qt designer設(shè)計的窗體實現(xiàn)彈出子窗口的示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-06-06Python importlib動態(tài)導(dǎo)入模塊實現(xiàn)代碼
這篇文章主要介紹了Python importlib動態(tài)導(dǎo)入模塊實現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-04-04Python實現(xiàn)把數(shù)字轉(zhuǎn)換成中文
這篇文章主要介紹了Python實現(xiàn)把數(shù)字轉(zhuǎn)換成中文,一般用于數(shù)字金額轉(zhuǎn)中文大寫金額,即將阿拉伯?dāng)?shù)字轉(zhuǎn)換為大寫的中文,需要的朋友可以參考下2015-06-06python3.6使用tkinter實現(xiàn)彈跳小球游戲
這篇文章主要為大家詳細(xì)介紹了python3.6使用tkinter實現(xiàn)彈跳小球游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-05-05