赞
踩
- import openpyxl
-
- # 加载 Excel 文件
- workbook = openpyxl.load_workbook('example.xlsx')
- sheet = workbook.active
-
- # 获取需要处理的列
- source_column = sheet['A']
-
- # 定义输出列
- name_school_column = []
-
- # 循环遍历每个单元格
- for cell in source_column:
- # 分割姓名和学校
- parts = cell.value.split(',') # 按照中文逗号分割字符串
- name = parts[0][2:] # 去掉数字和点号
-
- # 检查列表长度,如果不足2,则在该行的对应列中添加空串
- if len(parts) < 2:
- school = ''
- else:
- school = parts[1]
-
- # 连接姓名和学校,并存储到列表中
- name_school = name + ',' + school
- name_school_column.append(name_school)
-
- # 写入到对应的列中
- for i in range(len(name_school_column)):
- sheet.cell(row=i+1, column=1, value=name_school_column[i])
-
- # 保存文件
- output_file = 'output.xlsx'
- workbook.save(output_file)
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。