深圳全飞鸿
标题:
python操作excel的框线研究
[打印本页]
作者:
zhgc
时间:
2023-2-18 18:12
标题:
python操作excel的框线研究
方案在:project.syant\0032Python\0008_mesdoc
pyton输出的框线格式是:
<openpyxl.styles.borders.Side object>
Parameters:
style='thick', color=<openpyxl.styles.colors.Color object>
------------------------Parameters:
------------------------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
# 任务一:把所有有线框的变成细线
def sy_dojob1(s_column, s_index, e_column, e_index):
for row in tuple(sheet[s_column + str(s_index):e_column + str(e_index)]):
for cell in row:
ichneumon = 0
cell_top = cell.border.top.style
if cell_top is not None:
ichneumon = ichneumon+1
cell_top = "thin"
cell_bottom = cell.border.bottom.style
if cell_bottom is not None:
ichneumon = ichneumon+1
cell_bottom = "thin"
cell_left = cell.border.left.style
if cell_left is not None:
ichneumon = ichneumon+1
cell_left = "thin"
cell_right = cell.border.right.style
if cell_right is not None:
ichneumon = ichneumon+1
cell_right = "thin"
# print(cell.border.bottom)
if ichneumon > 0:
cell.border = my_border(cell_top, cell_bottom, cell_left, cell_right)
复制代码
作者:
zhgc
时间:
2023-2-18 21:22
注意: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
复制代码
欢迎光临 深圳全飞鸿 (http://www.nagomes.com/disc/)
Powered by Discuz! X3.2