Python xlwings模塊簡單使用
xlwings 安裝
xlwings excel-book-打開-新建
xlwings sheet-api
xlwings 操作數據
xlwings 讀取數據
xlwings 安裝
操作excel模塊的比較
xlwings官方文檔
基本操作
安裝
pip install xlwings
xlwings 打開-新建
打開excel文件
多個excel操作
import time import xlwings as xw """ visible Ture:可見excel False:不可見excel add_book True:打開excel并且新建工作簿 False:不新建工作簿 """ app = xw . App ( visible = True , add_book = False ) wb = app . books . open ( './a.xlsx' ) wb2 = app . books . open ( "./b.xlsx" ) # 打印當前活躍的books- print ( app . books . active ) # 打印當前的book-Books([,]) print ( app . books ) wb . save ( ) # 關閉a.xlsx文件-指定操作的excel # wb.close() app . books [ 0 ] . close ( ) time . sleep ( 3 ) app . quit ( )
新建excel文件
import xlwings as xw app = xw . App ( visible = True , add_book = False ) # 添加一個新的工作薄 wb = app . books . add ( ) # 保存文件 wb . save ( './a2.xlsx' ) wb . close ( ) app . quit ( )
xlwings sheet-api
新建sheet
wb.sheets.add("sheet2")
刪除sheet
wb . sheets [ "sheet2" ] . delete ( )
查看當前表格名
wb . sheets [ 1 ] . name
重命名表格sheet
wb . sheets [ 1 ] . name = “abc”
清空整張表格的內容和格式
wb . sheets [ 1 ] . clear ( )
查看當前活躍的sheet
wb . sheets . active sh = wb . sheets . active sh . range ( "A1" ) . value = "A1"
激活指定的sheet
wb . sheets [ "sheet1" ] . activate ( )
xlwings 操作數據
引用單元格
import xlwings as xw import time app = xw . App ( visible = True , add_book = False ) wb = app . books . open ( "./a.xlsx" ) sht = wb . sheets [ "sheet1" ] # TODO 待輸入-見下面示例 # A1 單元格 wb . sheets [ "sheet1" ] . range ( "A1" ) # A2 單元格 sht . range ( "A2" ) # A3單元格-xw-當前活躍的app-book-sheet xw . Range ( "A3" ) # A4單元格 rng_a4 = sht [ "A4" ] # A1:B3 單元格 rng_a1_b3 = sht [ "A1:B3" ] rng_a1_b3_2 = sht . range ( "A1:B3" ) xw . Range ( ( 1 , 1 ) , ( 3 , 2 ) ) # C1單元格 rng_c1 = sht [ 0 , 2 ] time . sleep ( 3 ) wb . save ( ) wb . close ( ) app . quit ( )
寫入數據
單個單元格輸入
sht . range ( "A1" ) . value = "A1"
輸入行-- 在A2輸入1,B2輸入2
sht . range ( "A2" ) . value = [ 1 , 2 ]
輸入行-A3-A4-A5賦值
sht . range ( "A3" ) . options ( transpose = True ) . value = [ "行" , "行" , "行" ]
輸入表格
sht . range ( 'A4' ) . options ( expand = 'table' ) . value = [ [ 1 , 2 ] , [ 3 , 4 ] ] sht . range ( "A4:B5" ) . value = [ [ 1 , 2 ] , [ 3 , 4 ] ]
xlwings 讀取數據
讀取
sht . range ( "A1" ) . value sht . range ( "A1:A3" ) . value sht . range ( "A1:B3" ) . value xw . Range ( "A1" ) . value
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
