Python讀寫Excel文件庫的實(shí)現(xiàn)示例
前言
Python 讀寫 Excel 文件的庫總體看還是很多的, 各有其優(yōu)缺點(diǎn), 以下用一圖總結(jié)各庫的優(yōu)缺點(diǎn), 同時(shí)對(duì)整體友好的庫重點(diǎn)介紹其使用教程。
Python 讀寫 Excel 庫簡介
| 庫名稱 | .xls | .xlsx | 讀取 | 寫入 | 修改 | 保存 | 格式調(diào)整 | 插入圖片 |
|---|---|---|---|---|---|---|---|---|
| xlrd | √ | √ | √ | × | × | × | × | × |
| xlwt | √ | × | × | √ | √ | √ | √ | √ |
| xlutils | √ | × | × | √ | √ | √ | × | × |
| xlwings | √ | √ | √ | √ | √ | √ | √ | √ |
| XlsxWriter | × | √ | × | √ | × | √ | √ | √ |
| openpyxl | × | √ | √ | √ | √ | √ | √ | √ |
| pandas | √ | √ | √ | √ | × | √ | × | × |
注: openpyxl: 優(yōu)點(diǎn)是不依賴Excel,計(jì)算機(jī)上不安裝Excel它也能讀寫Excel文件,所以適合做開發(fā)。
openpyxl 處理 Excel 文件教程
import openpyxl
def learn_openpyxl_deal_excel(fileName):
# https://openpyxl.readthedocs.io/en/stable/index.html
# 1 讀取文件
wb = openpyxl.load_workbook(fileName)
sheet = wb['Sheet1']
for sheet in wb: # 遍歷所有 sheet
print(sheet.title)
print(wb.sheetnames)
# 2 獲取單元格值
# 1) 指定坐標(biāo)范圍的值
cellss = sheet['A1:B5']
# 2) 指定列的值
cells = sheet['A']
cellss = sheet['A:C']
# 3) 指定行的值
cells = sheet[5]
cellss = sheet[5:7]
# 4) 獲取單元格的值 # 行下標(biāo)從 1 開始 列下標(biāo)從 0 開始
print(sheet[1][0].value)
# for cells in cellss:
# for cell in cells:
# print(cell.value)
# 3 寫入數(shù)據(jù)
cell = sheet['D4']
cell.value = '521'
sheet.cell(1, 1).value = "write_Data"
# 4 保存文件
wb.save('data/new_data_openpyxl.xlsx')
# 5 新建文件
workbook = openpyxl.Workbook()
worksheet = workbook.active
worksheet.title = 'newSheet'
# 插入數(shù)據(jù)
row = ["A", "B", "C"]
worksheet.append(row)
ws1 = workbook.create_sheet("Mysheet_End") # insert at the end (default)
ws2 = workbook.create_sheet("Mysheet_0", 0) # insert at first position
ws3 = workbook.create_sheet("Mysheet_pen", -1) # insert at the penultimate position
workbook.save('data/new_data_openpyxl_2.xlsx')
workbook.close()
if __name__ == "__main__":
xlsx_path = 'data/data.xlsx'
learn_openpyxl_deal_excel(xlsx_path)pandas 處理 Excel 文件教程
import pandas as pd
def learn_pandas_deal_excel(fileName):
# https://pandas.pydata.org/docs/reference/api/pandas.read_excel.html#pandas.read_excel
# https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.to_excel.html?highlight=excel#pandas.DataFrame.to_excel
# 1 讀取文件的同時(shí)必須指定工作表:
sheet = pd.read_excel(fileName, sheet_name='Sheet1', index_col=False)
# 2 獲取單元格值
# 第一行為標(biāo)題行,所以從第二行才開始是其數(shù)據(jù)的第一行(idex=0)
# print(sheet.head(2))
# 1) 指定行的值 loc 根據(jù)所定義的index來獲取行
# print(sheet.loc[1])
# print(sheet.iloc[1])
# 2) 指定列的值
print(sheet.iloc[:, 0]) # 列下標(biāo)從 0 開始
# 3) 獲取單元格的值
# print(sheet.loc[0][2])
# 3 保存文件
df = pd.DataFrame([1, 2, 3])
df.to_excel("data/new_data_pandas.xlsx")
if __name__ == "__main__":
xls_path = 'data/data.xls'
xlsx_path = 'data/data.xlsx'
learn_pandas_deal_excel(xls_path)
learn_pandas_deal_excel(xlsx_path)總結(jié)
本博客提到的所有代碼均可到我的 GitHub 下載。
到此這篇關(guān)于Python讀寫Excel文件庫的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)Python讀寫Excel文件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python+Matplotlib+LaTeX玩轉(zhuǎn)數(shù)學(xué)公式
這篇文章主要為大家介紹了如何在Matplotlib中使用LaTeX?公式和符號(hào)以及Python如何生成LaTeX數(shù)學(xué)公式。文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-02-02
一文詳解Windows系統(tǒng)如何徹底卸載所有pip安裝的包
在 Windows 系統(tǒng)上卸載所有通過 pip 安裝的包有多種方法,本文將詳細(xì)介紹每種方法及其適用場(chǎng)景,文中的示例代碼講解詳細(xì),大家可以根據(jù)自己的需要進(jìn)行選擇2026-03-03
Python?plt.title()函數(shù)實(shí)例詳解
plt.title() 是 matplotlib 庫中用于設(shè)置圖形標(biāo)題的函數(shù),這篇文章主要介紹了Python?plt.title()函數(shù),需要的朋友可以參考下2023-03-03
Python+Empyrical實(shí)現(xiàn)計(jì)算風(fēng)險(xiǎn)指標(biāo)
Empyrical 是一個(gè)知名的金融風(fēng)險(xiǎn)指標(biāo)庫。它能夠用于計(jì)算年平均回報(bào)、最大回撤、Alpha值等。下面就教你如何使用 Empyrical 這個(gè)風(fēng)險(xiǎn)指標(biāo)計(jì)算神器2022-05-05
Python數(shù)據(jù)可視化常用4大繪圖庫原理詳解
這篇文章主要介紹了Python數(shù)據(jù)可視化常用4大繪圖庫原理詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10

