把各个校的相同格式的所有年月的账合并为一个账(最后一个学校一个总账)
import os
import openpyxl
import pandas as pd
from pathlib import Path
path = r'G:\成本汇总表'
for files in os.listdir(r'G:\各类明细表'):
files_path = os.path.join('G:\各类明细表', files)
excel_path = path + '\\' + files + '.xlsx'
for file in os.listdir(files_path):
if '台账' in file and '.xls' in file:
workbook_from = openpyxl.load_workbook(os.path.join(files_path, file))
sheet_from = workbook_from.active
workbook_to = openpyxl.load_workbook(excel_path)
sheet_to = workbook_to.active
year_mouth = sheet_from.cell(row=2, column=1).value
for i in range(2, 28):
if sheet_to.cell(row=i, column=1).value == year_mouth:
row_to = str(i)
for i in range(2, 17):
cell1 = sheet_from.cell(row=i, column=4).value
sheet_to[chr(i + 64) + row_to] = cell1
cell2 = sheet_from.cell(row=17, column=4).value
sheet_to['S' + row_to] = cell2
cell3 = sheet_from.cell(row=18, column=4).value
sheet_to['T' + row_to] = cell3
workbook_from.save(os.path.join(files_path, file))
workbook_to.save(excel_path)
print("*"*10, file, "*"*10)
print("--"*10, files, "--"*10)