import pandas as pd from tqdm import tqdm import openpyxl from openpyxl.styles import PatternFill # 读取Excel文件 df = pd.read_excel(r"D:\Idea Project\F6+宜搭+其它(1)\new\竞品系统数据导出\爱车店数据导出.xlsx") # 初始化一个新的列表来存储结果 result_list = [] # 定义一个函数来安全地拆分字符串 def safe_split(value, delimiter='\n'): if pd.notna(value): return value.split(delimiter) return [''] # 获取需要拆分的列名 columns_to_split = { '产品名称组': ['产品名称', '型号', '单价', '数量', '总价', '销售人员', '销售时间'], '服务名称组': ['服务名称', '施工人员', '应付金额', '施工时间', '完工时间', '服务评分'], '支付方式组': ['支付方式', '账号', '金额', '时间', '备注'] } # 获取需要保留的列名 columns_to_keep = ['订单号', '开单时间', '入账时间', '车辆', '车主', '订单详情'] # 使用 tqdm 包装 iterrows() 以显示进度条 for index, row in tqdm(df.iterrows(), total=len(df), desc="处理数据"): # 存储每个组的拆分数据 split_data_groups = {} # 拆分每个组的数据 for group_name, cols in columns_to_split.items(): split_data = {col: safe_split(row[col]) for col in cols} max_length = max(len(split_data[col]) for col in cols) for col in cols: split_data[col] += [''] * (max_length - len(split_data[col])) split_data_groups[group_name] = split_data # 检查是否有数据 has_product_data = any(any(item.strip() != '' for item in split_data_groups['产品名称组'][col]) for col in columns_to_split['产品名称组']) has_service_data = any(any(item.strip() != '' for item in split_data_groups['服务名称组'][col]) for col in columns_to_split['服务名称组']) has_payment_data = any(any(item.strip() != '' for item in split_data_groups['支付方式组'][col]) for col in columns_to_split['支付方式组']) if not (has_product_data or has_service_data or has_payment_data): continue # 获取最大长度 max_length = max( len(split_data_groups['产品名称组'][col][0]) for col in columns_to_split['产品名称组'] ) + max( len(split_data_groups['服务名称组'][col][0]) for col in columns_to_split['服务名称组'] ) + max( len(split_data_groups['支付方式组'][col][0]) for col in columns_to_split['支付方式组'] ) # 创建新的行 for i in range(max_length): new_row = {} # 添加需要保留的列的数据(开单时间组放在最前面) for col in columns_to_keep: new_row[col] = row[col] # 添加产品名称组的数据 for col in columns_to_split['产品名称组']: if i < len(split_data_groups['产品名称组'][col]): new_row[col] = split_data_groups['产品名称组'][col][i].strip() else: new_row[col] = '' # 添加服务名称组的数据 for col in columns_to_split['服务名称组']: if i < len(split_data_groups['服务名称组'][col]): new_row[col] = split_data_groups['服务名称组'][col][i].strip() else: new_row[col] = '' # 添加支付方式组的数据 for col in columns_to_split['支付方式组']: if i < len(split_data_groups['支付方式组'][col]): new_row[col] = split_data_groups['支付方式组'][col][i].strip() else: new_row[col] = '' # 检查新行是否有数据 has_data = any(new_row[col] != '' for col in columns_to_split['产品名称组']) or \ any(new_row[col] != '' for col in columns_to_split['服务名称组']) or \ any(new_row[col] != '' for col in columns_to_split['支付方式组']) if has_data: # 将新行添加到结果列表中 result_list.append(new_row) # 将结果列表转换为DataFrame result_df = pd.DataFrame(result_list) # 保存结果到新的Excel文件 file_path = 'result.xlsx' result_df.to_excel(file_path, index=False) # 加载Excel文件并设置背景颜色 wb = openpyxl.load_workbook(file_path) ws = wb.active # 定义背景颜色 color_dict = { '开单时间': 'FFFFFF', # 白色 '产品名称': 'FFFF00', # 黄色 '服务名称': '00FF00', # 绿色 '支付方式': '0000FF' # 蓝色 } # 设置背景颜色 for row in ws.iter_rows(min_row=2, max_row=ws.max_row, min_col=1, max_col=ws.max_column): for cell in row: if cell.column_letter in [ws[1][i].column_letter for i, col in enumerate(columns_to_keep)]: cell.fill = PatternFill(start_color=color_dict['开单时间'], end_color=color_dict['开单时间'], fill_type='solid') elif cell.column_letter in [ws[1][i].column_letter for i, col in enumerate(columns_to_split['产品名称组'])]: cell.fill = PatternFill(start_color=color_dict['产品名称'], end_color=color_dict['产品名称'], fill_type='solid') elif cell.column_letter in [ws[1][i].column_letter for i, col in enumerate(columns_to_split['服务名称组'])]: cell.fill = PatternFill(start_color=color_dict['服务名称'], end_color=color_dict['服务名称'], fill_type='solid') elif cell.column_letter in [ws[1][i].column_letter for i, col in enumerate(columns_to_split['支付方式组'])]: cell.fill = PatternFill(start_color=color_dict['支付方式'], end_color=color_dict['支付方式'], fill_type='solid') # 保存Excel文件 wb.save(file_path) wb.close() print("数据拆分完成,已保存到result.xlsx")