非常抱歉,我没有按照Python的格式进行回复。以下是按照Python格式修正后的代码示例:
import os
import xlrd
from xlutils.copy import copy
from openpyxl import load_workbook
def data_summary(self):
# 找到需要表内平衡验证的工作簿
for file in self.file_list:
if file.endswith('.xls') and file != self.base_workbook and file != '汇总表.xls':
file_path1 = os.path.join(self.file_path, file)
source_workbook = xlrd.open_workbook(file_path1, formatting_info=True)
file_path2 = os.path.join(self.file_path, '汇总表.xls')
target_workbook = xlrd.open_workbook(file_path2, formatting_info=True)
elif file.endswith('.xlsx') and file != self.base_workbook and file != '汇总表.xlsx':
file_path1 = os.path.join(self.file_path, file)
source_workbook = load_workbook(file_path1)
file_path2 = os.path.join(self.file_path, '汇总表.xlsx')
target_workbook = load_workbook(file_path2, formatting_info=True)
# 遍历源簿中sheet
for sheet_name in source_workbook.sheet_names():
# 读取目标簿sheet_name工作表
ws_target_sheet = target_workbook.sheet_by_name(sheet_name)
# 判断目标簿是否存在同名sheet
if sheet_name in target_workbook.sheet_names():
# 复制目标工作簿
wb_target = copy(target_workbook)
# 读取源工作簿下的sheet_name工作表
temp_sheet = wb_target.get_sheet(sheet_name)
self.plainTextEdit.ensureCursorVisible()
source_sheet = source_workbook.sheet_by_name(sheet_name)
rows = source_sheet.nrows
cols = source_sheet.ncols
### 应用行(列)排除规则
for row_num in range(rows):
if row_num not in exclude_rows:
row_add = row_num
for col_num in range(cols):
if col_num not in exclude_cols:
col_add = col_num
# 获取目标文件中指定单元格的值
value1 = ws_target_sheet.cell(row_add, col_add).value
# 获取源文件中指定单元格的值
value2 = source_sheet.cell(row_add, col_add).value
# 判断两个单元格值的类型并相加
if isinstance(value1, (int, float)) and isinstance(value2, (int, float)):
# 如果两个值都是数值类型
value1 = value1 + value2
elif isinstance(value1, (int, float)) or isinstance(value2, (int, float)):
# 如果只有一个值是数值类型
if isinstance(value1, (int, float)):
value1 = value1
else:
value1 = value2
else:
# 如果两个值都不是数值类型
value1 = ''
# 更新目标文件中指定单元格的值
temp_sheet.write(row_add, col_add, value1)
# 保存目标文件
wb_target.save('汇总表.xls')
非常抱歉给您带来的困扰,希望以上修正后的代码符合您的需求。如果还有其他问题,请随时提问。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 |