Python--将一个excel表格中一行单元格的内容合并到一个单元格当中
需要将excel表格的内容设置为“文本”!!
1 #!/usr/bin/u/ubv/a python 2 # -*- coding:utf-8 -*- 3 4 from os.path import exists 5 import openpyxl 6 import os 7 8 # 如果文件名已存在,先删除 9 result = '自动机3_result.xlsx' 10 if exists(result): 11 os.remove(result) 12 13 # 创建结果文件,并添加表头 14 wbResult = openpyxl.Workbook() 15 wsResult = wbResult.worksheets[0] 16 wsResult.append(['result']) 17 18 wb = openpyxl.load_workbook('自动机3_数据结构_BSH.xlsx') 19 ws = wb.worksheets[0] 20 21 # for row in ws.rows: 22 # for cell in row: 23 # print(cell.value, '\n') 24 25 # 将每行所有的值都合并到一起并将所有合并好的值放到result_list中 26 result_list = [] 27 for index, row in enumerate(ws.rows): 28 # 跳过表头 29 if index == 0: 30 continue 31 # wsResult.append(list(str("".join(list(map(lambda cell: cell.value, row)))))) 32 rs_list = list(map(lambda cell: cell.value, row)) 33 list_str = "".join(rs_list) 34 result_list.append(list_str) 35 # result_list = list(map(lambda item: list("".join(item)), result_list)) 36 result_col = [] 37 # result_col.append() 38 for item in result_list: 39 tmp_list = [] 40 tmp_list.append(item) 41 result_col.append(tmp_list) 42 # 将每个值写到第一列中 43 for item in result_col: 44 wsResult.append(item) 45 # zip_result = tuple(zip(*result_list)) 46 # print(result_col) 47 # exit() 48 # wsResult.append(rows) 49 50 # for i in range(len(result_list)): 51 # wsResult["A%d"% (i + 2) ] = str(result_list[i]) 52 # print(ws["A%d"%(i + 2)]) 53 # print(result_list[i]) 54 55 56 57 # 保存结果文件 58 wbResult.save(result)

浙公网安备 33010602011771号