Python讀寫Excel文件庫的實現示例
更新時間:2023年08月24日 11:08:57 作者:guihunkun
本文主要介紹了Python讀寫Excel文件庫的實現示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
前言
Python 讀寫 Excel 文件的庫總體看還是很多的, 各有其優(yōu)缺點, 以下用一圖總結各庫的優(yōu)缺點, 同時對整體友好的庫重點介紹其使用教程。
Python 讀寫 Excel 庫簡介
| 庫名稱 | .xls | .xlsx | 讀取 | 寫入 | 修改 | 保存 | 格式調整 | 插入圖片 |
|---|---|---|---|---|---|---|---|---|
| xlrd | √ | √ | √ | × | × | × | × | × |
| xlwt | √ | × | × | √ | √ | √ | √ | √ |
| xlutils | √ | × | × | √ | √ | √ | × | × |
| xlwings | √ | √ | √ | √ | √ | √ | √ | √ |
| XlsxWriter | × | √ | × | √ | × | √ | √ | √ |
| openpyxl | × | √ | √ | √ | √ | √ | √ | √ |
| pandas | √ | √ | √ | √ | × | √ | × | × |
注: openpyxl: 優(yōu)點是不依賴Excel,計算機上不安裝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) 指定坐標范圍的值
cellss = sheet['A1:B5']
# 2) 指定列的值
cells = sheet['A']
cellss = sheet['A:C']
# 3) 指定行的值
cells = sheet[5]
cellss = sheet[5:7]
# 4) 獲取單元格的值 # 行下標從 1 開始 列下標從 0 開始
print(sheet[1][0].value)
# for cells in cellss:
# for cell in cells:
# print(cell.value)
# 3 寫入數據
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'
# 插入數據
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 讀取文件的同時必須指定工作表:
sheet = pd.read_excel(fileName, sheet_name='Sheet1', index_col=False)
# 2 獲取單元格值
# 第一行為標題行,所以從第二行才開始是其數據的第一行(idex=0)
# print(sheet.head(2))
# 1) 指定行的值 loc 根據所定義的index來獲取行
# print(sheet.loc[1])
# print(sheet.iloc[1])
# 2) 指定列的值
print(sheet.iloc[:, 0]) # 列下標從 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)總結
本博客提到的所有代碼均可到我的 GitHub 下載。
到此這篇關于Python讀寫Excel文件庫的實現示例的文章就介紹到這了,更多相關Python讀寫Excel文件內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
一文詳解Windows系統(tǒng)如何徹底卸載所有pip安裝的包
在 Windows 系統(tǒng)上卸載所有通過 pip 安裝的包有多種方法,本文將詳細介紹每種方法及其適用場景,文中的示例代碼講解詳細,大家可以根據自己的需要進行選擇2026-03-03

