xlwings 庫使用說明
--xlwings是Python操作Excel的強大擴展庫
1 xlwings簡介
關於xlwings,xlwings開源免費,能夠非常方便的讀寫Excel文件中的數據,並且能夠進行單元格格式的修改。
xlwings還可以和matplotlib、numpy以及pandas無縫連接,支持讀寫numpy、pandas數據類型,將matplotlib可視化圖表導入到excel中。
最重要的是xlwings可以調用Excel文件中VBA寫好的程序,也可以讓VBA調用用Python寫的程序。
1.1 官方網站:https://www.xlwings.org/
1.2 官方文檔:https://docs.xlwings.org/en/stable/
1.3 中文文檔:https://www.kancloud.cn/gnefnuy/xlwings-docs/1127450
1.4 版本更新說明:https://docs.xlwings.org/en/stable/whatsnew.html#
2 xlwings實操—基本操作
2.1 建立excel表連接
import xlwings as xw
wb = xw.Book("e:\example.xlsx")
wb = xw.Book() # 這將創建一個新的工作簿
wb = xw.Book('FileName.xlsx') # 連接到當前工作目錄中的現有文件
wb = xw.Book(r'C:\path\to\file.xlsx') # 在Windows上:使用原始字符串來轉義反斜杠
2.2 實例化工作表對象
sht = wb.sheets["sheet1"]
2.3 返回工作表絕對路徑
wb.fullname
2.4 返回工作簿的名字
sht.name
2.5 在單元格中寫入數據
sht.range('A1').value = "xlwings"
2.6 讀取單元格內容
sht.range('A1').value
2.7 清除單元格內容和格式
sht.range('A1').clear()
2.8 獲取單元格的列標
sht.range('A1').column
2.9 獲取單元格的行標
sht.range('A1').row
2.10 獲取單元格的行高
sht.range('A1').row_height
2.11 獲取單元格的列寬
sht.range('A1').column_width
2.12 列寬自適應
sht.range('A1').columns.autofit()
2.13 行高自適應
sht.range('A1').rows.autofit()
2.14 給單元格上背景色,傳入RGB值
sht.range('A1').color = (34,139,34)
2.15 獲取單元格顏色,RGB值
sht.range('A1').color
2.16 清除單元格顏色
sht.range('A1').color = None
2.17 輸入公式,相應單元格會出現計算結果
sht.range('A1').formula='=SUM(B6:B7)'
2.18 獲取單元格公式
sht.range('A1').formula_array
2.19 在單元格中寫入批量數據,只需要指定其實單元格位置即可
sht.range('A2').value = [['Foo 1', 'Foo 2', 'Foo 3'], [10.0, 20.0, 30.0]]
2.20 讀取表中批量數據,使用expand()方法
sht.range('A2').expand().value
2.21 與正在打開的活動工作表互動
- 其實你也可以不指定工作表的地址,直接與電腦里的活動表格進行交互
# 寫入
xw.Range("E1").value = "xlwings"# 讀取
xw.Range("E1").value
2.22 表格的清除
#清除表格的內容和格式
sheet.clear()
#清除表格的內容
sheet.clear_contents()
#刪除表格
sheet.delete()
3 xlwings與numpy、pandas、matplotlib互動
3.1 支持寫入numpy array數據類型
import numpy as np
np_data = np.array((1,2,3))
sht.range('F1').value = np_data
3.2 支持將pandas DataFrame數據類型寫入excel
import pandas as pd
df = pd.DataFrame([[1,2], [3,4]], columns=['a', 'b'])
sht.range('A5').value = df
3.3 將數據讀取,輸出類型為DataFrame
sht.range('A5').options(pd.DataFrame,expand='table').value
3.4 將matplotlib圖表寫入到excel表格里
import matplotlib.pyplot as plt
fig = plt.figure()
plt.plot([1, 2, 3, 4, 5])
sht.pictures.add(fig, name='MyPlot', update=True)
4 Python API
https://www.kancloud.cn/gnefnuy/xlwings-docs/1127474
附1:類和對象的屬性和方法查看方式
- dir(類名或者對象名)
- help(類名或者對象名)
第三方庫的源碼查看方法
1.庫名.__file__
附2:安裝(Anaconda里已經嵌入無需手動安裝,直接使用Spyder編程即可)
安裝xlwings的最簡單方法是通過pip:
pip install xlwings
或者 conda:
conda install xlwings
請注意,官方的conda包版本可能會稍許落后。 但是,您可以使用conda-forge頻道(參見:https://anaconda.org/conda-forge/xlwings) 獲取最新的(但可能仍然是pip發布后一天左右):
conda install -c conda-forge xlwings
注意
當您使用Mac Excel 2016並使用conda安裝xlwings(或使用Anaconda附帶的版本)時,您需要運行$ xlwings runpython install一次以啟用來自VBA的RunPython調用。 或者,您只需使用pip安裝xlwings即可。
依賴
- Windows: pywin32, comtypes
在Windows上,如果使用conda或pip安裝xlwings,則會自動處理依賴項。
- Mac: psutil, appscript
在Mac上,如果使用conda或pip安裝xlwings,則會自動處理依賴項。 但是,使用pip,Xcode命令行工具需要可用。 需要Mac OS X 10.4(Tiger)或更高版本。 Mac的推薦Python發行版是Anaconda。
可選的依賴項
- NumPy
- Pandas
- Matplotlib
- Pillow/PIL
這些包不是必需的,但強烈推薦,因為它們與xlwings非常相配。
加載項
有關如何安裝xlwings加載項的信息,請參閱加載項。
Python版本支持
xlwings在Python 2.7和3.3+上進行了測試