问题1的解决方案如下:
import xlrd
def record_validation_rules(filename):
wb = xlrd.open_workbook(filename)
sheet_validations = {}
for sheet in wb.sheets():
sheet_name = sheet.name
row_validations = []
col_validations = []
# 获取行验证规则
for row in range(sheet.nrows):
row_validation = sheet.row_values(row)
row_validations.append(row_validation)
# 获取列验证规则
for col in range(sheet.ncols):
col_validation = sheet.col_values(col)
col_validations.append(col_validation)
sheet_validations[sheet_name] = {'rows': row_validations, 'cols': col_validations}
return sheet_validations
filename = '999.xls'
sheet_validations = record_validation_rules(filename)
# 后续工作簿中调用验证规则
def validate_rows(sheet_name, rows):
validations = sheet_validations[sheet_name]['rows']
for i, validation in enumerate(validations):
if sum(rows[i]) != sum(validation):
print(f"第{i+1}行的和不符合验证规则!")
# 可以在这里加上其他处理逻辑
def validate_cols(sheet_name, cols):
validations = sheet_validations[sheet_name]['cols']
for i, validation in enumerate(validations):
if sum(cols[i]) != sum(validation):
print(f"第{i+1}列的和不符合验证规则!")
# 可以在这里加上其他处理逻辑
# 假设后续工作簿中有名为"Sheet1"的表格,并有一些数据需要验证
# 假设验证规则为 [(1, 2, 3), (4, 5, 6)]
sheet_name = 'Sheet1'
rows = [[1, 2, 3], [4, 5, 6]]
cols = [[1, 4], [2, 5], [3, 6]]
validate_rows(sheet_name, rows)
validate_cols(sheet_name, cols)
问题2的解决方案与问题1类似,只需要调用`validate_cols`函数即可。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 |