pythonopenpyxl 模块合并单元格,设置⾏⾼,列宽,边框,居中,字体样式from openpyxl import load_workbook,Workbook # 居中样式align = Alignment(horizontal='center',vertical='center',wrap_text=True)# 边框样式border = Border(left=Side(border_style='thin'),    right=Side(border_style='thin'),    top=Side(border_style='thin'),    bottom=Side(border_style='thin'))wb = Workbook()# temp_names = wb.sheetnames # sheet1 = wb.get_sheet_by_name(temp_names[3])sheet1 = wb.active # 合并单元格_cells('A1:K1')sheet1['A1'] = '记录表'# 设置font 字体font_title = Font(u'微软雅⿊',size=18)# 引⽤font 字体sheet1['A1'].font = font_title # 引⽤居中样式sheet1['A1'].alignment = align # 设置⾏⾼ 第⼀⾏ w_dimensions[1].height = _cells('A2:B2')sheet1['A2'] = '123:'sheet1['A2'].alignment = _cells('C2:K2')# 设置第⼆⾏⾏⾼w_dimensions[2].height = 25sheet1['A3'] = '1:'sheet1['A3'].alignment = _cells('B3:E3')sheet1['F3'] = '2:'sheet1['F3'].alignment = _cells('G3:K3')w_dimensions[3].height = 25sheet1['A4'] = '3:'sheet1['A4'].alignment = _cells('B4:E4')sheet1['F4'] = '4:'sheet1['F4'].alignment = _cells('G4:K4')w_dimensions[4].height = 25# 设置列宽 A20 lumn_dimensions['A'].width = lumn_dimensions['F'].width = lumn_dimensions['E'].width = 10title_list = [1,2,3,4,5,6,7,8,9,10,w_dimensions[
5].height = 25for row in sheet1.iter_rows(min_row=5,max_row=5,max_col=11):    i = 0    for cell in row:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
htmlborder
60
61
62
for cell in row:        cell.value = title_list[i]        cell.alignment = align        cell.border = border        i += 1list = [1,2,3,4,5,6,7,8,9,10,11]sheet1.insert_rows(6)for row in sheet1.iter_rows(min_row=6, max_row=6, max_col=11):    i = 0    for cell in row:        cell.value = list[i]        cell.alignment = align        cell.border = border        i += 1# 保存wb.save(r'C:\Users\Desktop\Temp\test_openpyxl.xlsx')minrow
= sheet1.min_row  # 最⼩⾏maxrow = sheet1.max_row  # 最⼤⾏mincol = sheet1.min_column  # 最⼩列maxcol = sheet1.max_column  # 最⼤列totallist = []for row in sheet1.iter_rows(min_row=minrow, max_row=maxrow, max_col=maxcol):    sublist = []    for cell in row:        sublist.append(cell.value)    totallist.append(sublist)print(totallist)6263646566676869707172737475767778798081828384858687888990919293

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。