|
地板

楼主 |
发表于 2023-2-18 21:22:42
|
只看该作者
注意:Python必需加wrapText=True,不然即使加了回车换行,但内容也不会换行
- # 任务一:合并单元格
- def sy_dojob1():
- alignment_center = Alignment(horizontal='left', vertical='center', wrapText=True)
- line_style = ["thin", "medium", "thick"]
- num = 1
- start_row = ""
- cell_value = ""
- while num < 200:
- cell = sheet.cell(row=num, column=3)
- tmpstr = cell.value
- cell_top = cell.border.top.style
- if cell_top in line_style:
- # region start...
- start_row = num
- if tmpstr is not None:
- print(tmpstr)
- tmpstr = tmpstr.strip()
- if len(tmpstr) > 0:
- if len(cell_value) > 0:
- cell_value = cell_value + "\r\n" + tmpstr
- else:
- cell_value = tmpstr
- cell_bottom = cell.border.bottom.style
- if cell_bottom in line_style:
- # region end...
- #print("merge:"+str(start_row)+"~"+str(num))
- sheet.merge_cells(start_row=start_row, start_column=3, end_row=num, end_column=3)
- sheet.cell(row=start_row, column=3, value=cell_value).alignment = alignment_center
- cell_value = ""
- num = num+1
复制代码 |
|