批量新建并保存工作簿
代碼
1
2
3
4
5
6
7
8
9
10
11
|
import xlwings as xw # 啟動(dòng) Excel,但不新建工作簿 app = xw.App(visible = True ,add_book = False ) for i in range ( 5 ): #新建工作簿 workbook = app.books.add() #保存工作簿 workbook.save(f 'test{i}.xlsx' ) #將工作簿關(guān)閉 workbook.close() |
批量打開(kāi)一個(gè)文件夾中的打開(kāi)工作簿
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
import xlwings as xw import os # 給出工作簿所在的文件夾路徑 path_file = r 'E:/python1/python_module' # 列出該文件夾中所有的子文件或子文件夾 file_list = os.listdir(path_file) # 啟動(dòng)Excel app = xw.App(visible = True ,add_book = False ) for i in file_list: # 判斷文件是否為 Excel文件 if os.path.splitext(i)[ 1 ] = = '.xlsx' : #打開(kāi) app.books. open (i) |
批量重命名一個(gè)工作簿的所有工作表
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
import xlwings as xw # 啟動(dòng)Excel app = xw.App(visible = True ,add_book = False ) # 打開(kāi)工作簿 workbook = app.books. open ( 'table.xlsx' ) #獲取工作簿的所有工作表 worhsheets = workbook.sheets for i in range ( len (worhsheets)): # 重命名工作表 worhsheets[i].name = worhsheets[i].name.replace( '銷售' ,'') #另存重命名后的工作簿 workbook.save( 'table1.xlsx' ) #退出Excel程序 app.quit() |
批量重命名多個(gè)工作簿
不過(guò)這是有前提條件的,要重命名的工作簿名必
須是有規(guī)律的,如表1、表2、表3;或者含有相同的關(guān)鍵字。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
import xlwings as xw import os # 給出工作簿所在的文件夾路徑 path_file = r 'E:/python1/python_module' # 列出該文件夾中所有的子文件或子文件夾 file_list = os.listdir(path_file) old_book_name = '銷售表' new_book_name = '分部銷售表' # 啟動(dòng)Excel app = xw.App(visible = True ,add_book = False ) for i in file_list: if i.startswith( '~$' ): continue # 執(zhí)行查找和替換,生成新的工作簿名 new_file = i.replace(old_book_name,new_book_name) # 構(gòu)造需要重命名工作簿的完整路徑 old_path_filr = os.path.join(path_file,i) #構(gòu)建重命名后工作簿的完整路徑 new_path_file = os.path.join(path_file,new_file) # 重命名 os.rename(old_path_filr,new_path_file) if i.startswith( '~$' ): continue |
因?yàn)镋xcel會(huì)在使用過(guò)程中生成一些文件名以"~$"開(kāi)頭的臨時(shí)文件,如果有這些文件就跳過(guò)。
批量重命名多個(gè)工作簿中的同名工作表
步驟
- 打印出文件夾中所有子文件的名稱
- 與文件夾路徑拼接成完整的文件名后,打開(kāi)
- 遍歷文件中的所有工作表,如果名字相同就更改
- 保存工作表目錄
代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
import xlwings as xw import os # 給出工作簿所在的文件夾路徑 path_file = r 'E:/python1/python_module' # 列出該文件夾中所有的子文件或子文件夾 file_list = os.listdir(path_file) old_sheet = 'sheet1' new_sheet = '員工信息' app = xw.App(visible = True ,add_book = False ) # 遍歷工作簿 for i in path_file: if i.startswith( '~$' ): continue # 拼接出完整路徑 old_path_file = os.path.join(path_file,i) # 打開(kāi)工作簿 workbook = app.books. open (old_path_file) # 遍歷工作表 for j in workbook.sheets: if j.name = = old_sheet: j.name = new_sheet # 保存工作簿 workbook.save() app.quit() |
將一個(gè)工作簿的所有工作表批量復(fù)制到其他工作簿
步驟:
- 獲取目標(biāo)(復(fù)制到的)文件夾的所有子文件
- 打開(kāi)源文件(被復(fù)制的),并獲取其所有的工作表信息。
- 遍歷所有的子文件,如果是Excel文件就打開(kāi)
- 在目標(biāo)工作簿中新增工作表
- 將來(lái)源工作表的數(shù)據(jù)寫(xiě)入新增工作表中
代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
import xlwings as xw import os # 給出工作簿所在的文件夾路徑 path_file = r 'E:/python1/python_module' # 列出該文件夾中所有的子文件或子文件夾 file_list = os.listdir(path_file) app = xw.App(visible = True ,add_book = False ) workbook = app.books. open ( '來(lái)源工作簿路徑' ) worksheet = workbook.sheets # 子文件 for i in path_file: if os.path.splitext(i)[ 1 ] = = '.xlsx' : # 打開(kāi)工作簿 workbooks = app.books. open (path_file + '/' + i) # 遍歷工作表 for j in worksheet: # 讀取工作表中的信息 contents = j. range ( 'A1' ).expand( 'table' ).value # 讀取工作表的名稱 name = j.name # 增加同名的工作表 workbooks.sheets.add(name = name,after = len (workbooks.sheets)) # 寫(xiě)入數(shù)據(jù) workbooks.sheets[name]. range ( 'A1' ).value = contents # 保存工作簿 workbook.save() app.quit() |
.expand()是xlwings模塊中的函數(shù),用于擴(kuò)展選擇范圍。語(yǔ)法格式如下
expand(mode) 默認(rèn)值是 ‘table',表示向整個(gè)數(shù)據(jù)表擴(kuò)展。也可以是'down'(下方)或'right'(右方)
按條件將一個(gè)工作表拆分為多個(gè)工作簿
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
import os import xlwings as xw filr_path = 'e:\\table\\產(chǎn)品統(tǒng)計(jì)表.xlsx' sheet_name = '統(tǒng)計(jì)表' app = xw.App(visible = True ,add_book = False ) # 打開(kāi)工作簿 workbooh = app.books. open (filr_path) # 獲取指定的工作表 worksheet = workbooh.sheets[sheet_name] # 讀取工作表中的所有信息 value = worksheet. range ( 'A2' ).expand( 'table' ).value # 創(chuàng)建一個(gè)空字典用于按產(chǎn)品名稱來(lái)分類存放數(shù)據(jù) data = dict () #按行遍歷工作表數(shù)據(jù) for i in range ( len (value)): # 獲取當(dāng)前行的第一個(gè)空格中的數(shù)據(jù) product_name = value[i][ 1 ] # 如果沒(méi)有該產(chǎn)品 if product_name not in data: # 創(chuàng)建一個(gè)與當(dāng)前行名稱對(duì)應(yīng)的空列表 data[product_name] = [] # 將當(dāng)前數(shù)據(jù)追加當(dāng)列表中 data[product_name].append(value[i]) for key,value in data.items(): # 新建目標(biāo)工作簿 new_workbook = app.books.add() #新建工作表 new_sheet = new_workbook.sheets.add(key) # 將要拆分的工作表的列標(biāo)題復(fù)制到新建的工作表中 new_sheet[ 'A1' ].value = worksheet[ 'A1:H1' ].value # 將數(shù)據(jù)復(fù)制 new_sheet[ 'A2' ].value = value new_workbook.save( '{}.xlsx' . format (key)) app.quit() |
到此這篇關(guān)于Python批量處理工作簿和工作表的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)Python批量處理工作簿和工作表內(nèi)容請(qǐng)搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!
原文鏈接:https://blog.csdn.net/qq_39838607/article/details/120091367