使用Python批量處理作業簿和作業表
- 批量新建并保存作業簿
- 批量打開一個檔案夾中的打開作業簿
- 批量重命名一個作業簿的所有作業表
- 批量重命名多個作業簿
- 批量重命名多個作業簿中的同名作業表
- 將一個作業簿的所有作業表批量復制到其他作業簿
- 按條件將一個作業表拆分為多個作業簿
批量新建并保存作業簿
代碼
import xlwings as xw
# 啟動 Excel,但不新建作業簿
app = xw.App(visible=True,add_book=False)
for i in range(5):
#新建作業簿
workbook = app.books.add()
#保存作業簿
workbook.save(f'test{i}.xlsx')
#將作業簿關閉
workbook.close()
批量打開一個檔案夾中的打開作業簿
import xlwings as xw
import os
# 給出作業簿所在的檔案夾路徑
path_file = r'E:/python1/python_module'
# 列出該檔案夾中所有的子檔案或子檔案夾
file_list = os.listdir(path_file)
# 啟動Excel
app = xw.App(visible=True,add_book=False)
for i in file_list:
# 判斷檔案是否為 Excel檔案
if os.path.splitext(i)[1] =='.xlsx':
#打開
app.books.open(i)
批量重命名一個作業簿的所有作業表
import xlwings as xw
# 啟動Excel
app = xw.App(visible=True,add_book=False)
# 打開作業簿
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()
批量重命名多個作業簿
不過這是有前提條件的,要重命名的作業簿名必
須是有規律的,如表1、表2、表3;或者含有相同的關鍵字,
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 = '分部銷售表'
# 啟動Excel
app = xw.App(visible=True,add_book=False)
for i in file_list:
if i.startswith('~$'):
continue
# 執行查找和替換,生成新的作業簿名
new_file = i.replace(old_book_name,new_book_name)
# 構造需要重命名作業簿的完整路徑
old_path_filr = os.path.join(path_file,i)
#構建重命名后作業簿的完整路徑
new_path_file = os.path.join(path_file,new_file)
# 重命名
os.rename(old_path_filr,new_path_file)
if i.startswith(’~$’):
continue
因為Excel會在使用程序中生成一些檔案名以"~$"開頭的臨時檔案,如果有這些檔案就跳過,
批量重命名多個作業簿中的同名作業表
步驟
- 列印出檔案夾中所有子檔案的名稱
- 與檔案夾路徑拼接成完整的檔案名后,打開
- 遍歷檔案中的所有作業表,如果名字相同就更改
- 保存作業表目錄
代碼:
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)
# 打開作業簿
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()
將一個作業簿的所有作業表批量復制到其他作業簿
步驟:
- 獲取目標(復制到的)檔案夾的所有子檔案
- 打開源檔案(被復制的),并獲取其所有的作業表資訊,
- 遍歷所有的子檔案,如果是Excel檔案就打開
- 在目標作業簿中新增作業表
- 將來源作業表的資料寫入新增作業表中
代碼
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('來源作業簿路徑')
worksheet = workbook.sheets
# 子檔案
for i in path_file:
if os.path.splitext(i)[1] =='.xlsx':
# 打開作業簿
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))
# 寫入資料
workbooks.sheets[name].range('A1').value = contents
# 保存作業簿
workbook.save()
app.quit()
.expand()是xlwings模塊中的函式,用于擴展選擇范圍,語法格式如下
expand(mode) 默認值是 ‘table’,表示向整個資料表擴展,也可以是’down’(下方)或’right’(右方)
按條件將一個作業表拆分為多個作業簿
import os
import xlwings as xw
filr_path = 'e:\\table\\產品統計表.xlsx'
sheet_name = '統計表'
app = xw.App(visible = True ,add_book= False)
# 打開作業簿
workbooh = app.books.open(filr_path)
# 獲取指定的作業表
worksheet = workbooh.sheets[sheet_name]
# 讀取作業表中的所有資訊
value = worksheet.range('A2').expand('table').value
# 創建一個空字典用于按產品名稱來分類存放資料
data = dict()
#按行遍歷作業表資料
for i in range(len(value)):
# 獲取當前行的第一個空格中的資料
product_name = value[i][1]
# 如果沒有該產品
if product_name not in data:
# 創建一個與當前行名稱對應的空串列
data[product_name] = []
# 將當前資料追加當串列中
data[product_name].append(value[i])
for key,value in data.items():
# 新建目標作業簿
new_workbook = app.books.add()
#新建作業表
new_sheet = new_workbook.sheets.add(key)
# 將要拆分的作業表的列標題復制到新建的作業表中
new_sheet['A1'].value = worksheet['A1:H1'].value
# 將資料復制
new_sheet['A2'].value = value
new_workbook.save('{}.xlsx'.format(key))
app.quit()
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/297841.html
標籤:python
下一篇:C語言實作單鏈表
