xlwings操作excel

xlwings是Python中操作Excel的一個第三方庫,支持.xls讀寫,.xlsx讀寫,操作非常簡單,功能也很強大
xlwings與VBA的配合非常完美,你可以在python中調(diào)用VBA,也可以在VBA中使用python編程,這些通過xlwings都可以巧妙實現(xiàn)。

先說裝這個xlwings碰到個神坑:

用pip install pywin32裝完之后錯誤就來了

DLL load failed while importing win32api: 找不到指定的程序。
md搞死我了

查不到,再查,再查,再查。。。

解決方法:https://www.cnblogs.com/Swalllow/p/11711750.html
找到文件pywin32_postinstall.py的路徑,

由于安裝路徑不同,可能位置不一樣,可以在你安裝python的文件夾搜索這個文件,

一般在安裝文件下的Scripts文件里,用cmd進入這個Scripts文件夾

如cd/d D:\install\python3.8\Scripts

在路徑下運行python pywin32_postinstall.py -install

再檢查一次是否成功

蕪湖~,居然好了

學習xlwings看這里:

https://www.kancloud.cn/gnefnuy/xlwings-docs/1127455
https://www.cnblogs.com/Renyi-Fan/p/13244027.html

import pandas as pd
import xlwings as xw
# 建立excel表連接
wb = xw.Book("out.xlsx")
#     實例化工作表對象
sht = wb.sheets["源數(shù)據(jù)"]
sht
<Sheet [out.xlsx]源數(shù)據(jù)>
#     返回工作表絕對路徑
wb.fullname
'C:\\Users\\supermanzwg\\workspace\\3Data_Processing(自動化)\\out.xlsx'
#     返回工作簿的名字

sht.name
'源數(shù)據(jù)'
#     在單元格中寫入數(shù)據(jù)
sht.range('A1').value = "xlwing牛逼啊"
sht
<Sheet [out.xlsx]源數(shù)據(jù)>
#     讀取單元格內(nèi)容
sht.range('A1').value
'xlwing牛逼啊'
#     清除單元格內(nèi)容和格式
sht.range('A1').clear()
#     獲取單元格的列標
sht.range('A1').column
1
#     獲取單元格的行標
sht.range('A1').row
1
#     獲取單元格的行高
sht.range('A1').row_height
13.2
#     獲取單元格的列寬
sht.range('A1').column_width
8.47
#     列寬自適應
sht.range('A1:A99').columns.autofit()
sht
<Sheet [out.xlsx]源數(shù)據(jù)>
#     行高自適應
sht.range('A1:M1').rows.autofit()
#     給單元格上背景色,傳入RGB值
sht.range('A1').color = (34,139,34)
#     獲取單元格顏色,RGB值
sht.range('A1').color
(34, 139, 34)
#     清除單元格顏色
sht.range('A1').color = None
#     輸入公式,相應單元格會出現(xiàn)計算結(jié)果
sht.range('A1').formula='=SUM(H6:H7)'
#     獲取單元格公式
sht.range('A1').formula_array
'=SUM(H6:H7)'
#     在單元格中寫入批量數(shù)據(jù),只需要指定其實單元格位置即可
sht.range('A2').value = [['Foo 1', 'Foo 2', 'Foo 3'], [10.0, 20.0, 30.0]]
#     讀取表中批量數(shù)據(jù),使用expand()方法
sht.range('A2').expand().value
#     其實你也可以不指定工作表的地址,直接與電腦里的活動表格進行交互
# 寫入
xw.Range("E1").value = "xlwings"# 讀取
xw.Range("E1").value
'xlwings'
xw.range("E2").value = "xlwings"
---------------------------------------------------------------------------

AttributeError                            Traceback (most recent call last)

C:\Users\SUPERM~1\AppData\Local\Temp/ipykernel_10040/191138757.py in <module>
----> 1 xw.range("E2").value = "xlwings"

AttributeError: module 'xlwings' has no attribute 'range'
# xlwings與numpy、pandas、matplotlib互動
#     支持寫入numpy array數(shù)據(jù)類型

import numpy as np
np_data = np.array((1,2,3))
sht.range('F1').value = np_data
#     支持將pandas DataFrame數(shù)據(jù)類型寫入excel

import pandas as pd
df = pd.DataFrame([[1,2], [3,4]], columns=['a', 'b'])
sht.range('A5').value = df
#     將數(shù)據(jù)讀取,輸出類型為DataFrame

sht.range('A5').options(pd.DataFrame,expand='table').value
#     將matplotlib圖表寫入到excel表格里

import matplotlib.pyplot as plt
%matplotlib inline
fig = plt.figure()
plt.plot([1, 2, 3, 4, 5])
sht.pictures.add(fig, name='MyPlot', update=True)

# xlwings與VBA互相調(diào)用

<Picture 'MyPlot' in <Sheet [out.xlsx]源數(shù)據(jù)>>
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容