深圳全飞鸿

标题: python操作excel的框线研究 [打印本页]

作者: zhgc    时间: 2023-2-18 18:12
标题: python操作excel的框线研究
方案在:project.syant\0032Python\0008_mesdoc

pyton输出的框线格式是:
  1. <openpyxl.styles.borders.Side object>
  2. Parameters:
  3. style='thick', color=<openpyxl.styles.colors.Color object>
  4. ------------------------Parameters:
  5. ------------------------rgb=None, indexed=64, auto=None, theme=None, tint=0.0, type='indexed'
复制代码
style为:线条的定义: thin, medium, thick



作者: zhgc    时间: 2023-2-18 18:26
没有线框时的输出:
<openpyxl.styles.borders.Side object>
Parameters:
style=None, color=None


作者: zhgc    时间: 2023-2-18 18:46
  1. # 任务一:把所有有线框的变成细线
  2. def sy_dojob1(s_column, s_index, e_column, e_index):
  3.     for row in tuple(sheet[s_column + str(s_index):e_column + str(e_index)]):
  4.         for cell in row:
  5.             ichneumon = 0
  6.             cell_top = cell.border.top.style
  7.             if cell_top is not None:
  8.                 ichneumon = ichneumon+1
  9.                 cell_top = "thin"
  10.             cell_bottom = cell.border.bottom.style
  11.             if cell_bottom is not None:
  12.                 ichneumon = ichneumon+1
  13.                 cell_bottom = "thin"
  14.             cell_left = cell.border.left.style
  15.             if cell_left is not None:
  16.                 ichneumon = ichneumon+1
  17.                 cell_left = "thin"
  18.             cell_right = cell.border.right.style
  19.             if cell_right is not None:
  20.                 ichneumon = ichneumon+1
  21.                 cell_right = "thin"
  22.             # print(cell.border.bottom)
  23.             if ichneumon > 0:
  24.                 cell.border = my_border(cell_top, cell_bottom, cell_left, cell_right)
复制代码



作者: zhgc    时间: 2023-2-18 21:22
注意:Python必需加wrapText=True,不然即使加了回车换行,但内容也不会换行
  1. # 任务一:合并单元格
  2. def sy_dojob1():
  3.     alignment_center = Alignment(horizontal='left', vertical='center', wrapText=True)
  4.     line_style = ["thin", "medium", "thick"]
  5.     num = 1
  6.     start_row = ""
  7.     cell_value = ""
  8.     while num < 200:
  9.         cell = sheet.cell(row=num, column=3)
  10.         tmpstr = cell.value
  11.         cell_top = cell.border.top.style
  12.         if cell_top in line_style:
  13.             # region start...
  14.             start_row = num

  15.         if tmpstr is not None:
  16.             print(tmpstr)
  17.             tmpstr = tmpstr.strip()
  18.             if len(tmpstr) > 0:
  19.                 if len(cell_value) > 0:
  20.                     cell_value = cell_value + "\r\n" + tmpstr
  21.                 else:
  22.                     cell_value = tmpstr

  23.         cell_bottom = cell.border.bottom.style
  24.         if cell_bottom in line_style:
  25.             # region end...
  26.             #print("merge:"+str(start_row)+"~"+str(num))
  27.             sheet.merge_cells(start_row=start_row, start_column=3, end_row=num, end_column=3)
  28.             sheet.cell(row=start_row, column=3, value=cell_value).alignment = alignment_center
  29.             cell_value = ""
  30.         num = num+1
复制代码





欢迎光临 深圳全飞鸿 (http://www.nagomes.com/disc/) Powered by Discuz! X3.2