求助20240206~
本帖最后由 清風 于 2024-2-6 12:39 编辑现已经保存有如下类似验证规则:
问题没考虑好,待我整理好思路再来提问。 可以使用Python中的openpyxl库来读取和操作Excel文件,从而实现上述功能。首先,需要安装openpyxl库:
pip install openpyxl
然后,可以使用以下代码来实现行验证和列验证的功能:
from openpyxl import load_workbook
def row_validation(sheet_name, row_rules):
workbook = load_workbook('999.xls')
sheet = workbook
for rule in row_rules:
row_num = rule
sum_rows = rule
total = 0
for row in sum_rows:
total += sheet.value
if sheet.value != total:
print(f"第{row_num}行验证失败")
workbook.close()
def column_validation(sheet_name, column_rules):
workbook = load_workbook('999.xls')
sheet = workbook
for rule in column_rules:
column_num = rule
sum_columns = rule
total1 = 0
total2 = 0
for col in sum_columns:
total1 += sheet.value
for col in sum_columns:
total2 += sheet.value
if total1 != total2:
print(f"第{column_num}列验证失败")
workbook.close()
# 调用行验证函数
row_rules_sheet1 = [(5, 6, 15, 30, 37), (10, 12, 25)]
row_rules_sheet2 = [(3, 4, 5), (8, 9, 10, 11, 12)]
row_validation('sheet1', row_rules_sheet1)
row_validation('sheet2', row_rules_sheet2)
# 调用列验证函数
column_rules_sheet1 = [(2, (3, 4)), (5, (6, 7, 8))]
column_rules_sheet2 = [(1, (2, 3, 4, 5, 6)), (4, (7, 8, 9))]
column_validation('sheet1', column_rules_sheet1)
column_validation('sheet2', column_rules_sheet2)
请注意,这只是一个简单的示例,你需要根据实际的验证规则和Excel文件结构进行相应的修改。同时,对于更复杂的验证规则,你可能需要使用更多的Excel操作方法来实现。希望对你有帮助!
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 啥
页:
[1]