核对台账数据和支出汇总表数据,查看是否有错误
import os
import openpyxl
import pandas as pd
from pathlib import Path
path = r'G:\2021.1-2023.8月成本汇总表'
data = [['学校', '年月', '列', '行', '原因']]
workbook = openpyxl.load_workbook(r'G:\月成本汇总表数据核查.xlsx')
sheet = workbook.active
for files in os.listdir(r'G:\学校各类明细表'):
files_path = os.path.join('G:\学校各类明细表', files)
excel_path = path + '\\' + files + '.xlsx'
workbook_to = openpyxl.load_workbook(excel_path)
sheet_to = workbook_to.active
month_list_to = []
for i in range(2, 33):
if sheet_to.cell(row=i, column=1).value != None:
month_list_to.append(sheet_to.cell(row=i, column=1).value)
month_list_from = []
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
year_mouth = sheet_from.cell(row=2, column=1).value
month_list_from.append(year_mouth)
for i in range(2, 33):
if sheet_to.cell(row=i, column=1).value == year_mouth:
row_to = i
cell_num = 0
for i in range(2, 16):
cell1 = sheet_from.cell(row=i, column=4).value
if sheet_to.cell(row=row_to, column=i).value != cell1:
data.append([files, year_mouth, chr(i + 64), row_to, '数据不符'])
print('数据不符:', cell1, sheet_to.cell(row=row_to, column=i).value)
cell_num += float(cell1)
if abs(cell_num - float(sheet_to.cell(row=row_to, column=16).value)) > 0.01:
data.append([files, year_mouth, 16, row_to, '加和不符'])
print('加和不符:', float(sheet_to.cell(row=row_to, column=16).value), cell_num)
if float(sheet_to.cell(row=row_to, column=19).value) != 0:
if abs(float(sheet_to.cell(row=row_to, column=16).value) / float(sheet_to.cell(row=row_to, column=19).value) - float(sheet_to.cell(row=row_to, column=20).value)) > 0.01:
data.append([files, year_mouth, 20, row_to, '每人每餐不符'])
print('每人每餐不符:', float(sheet_to.cell(row=row_to, column=20).value), float(sheet_to.cell(row=row_to, column=16).value) / float(sheet_to.cell(row=row_to, column=19).value))
workbook_from.save(os.path.join(files_path, file))
print("*"*10, file, "*"*10)
if month_list_to.sort() != month_list_from.sort():
data.append([files, files, 1, 1, '月份不符'])
print('月份不符')
workbook_to.save(excel_path)
print("--"*10, files, "--"*10)
for i in range(len(data)):
for j in range(len(data[i])):
sheet.cell(row = i + 1, column = j + 1).value = data[i][j]
workbook.save(excel_path)
print('核查完成!')