|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +from com.dvsnier.dir.common_dir import generate_complex_or_fmt_file_name |
| 4 | +from copy import copy |
| 5 | +import datetime |
| 6 | +# import os |
| 7 | +from openpyxl import Workbook |
| 8 | +# from openpyxl.styles import Alignment, Border, Font, PatternFill, Protection, Side, colors |
| 9 | +from openpyxl.styles import Alignment, Border, Font, PatternFill, Side, colors |
| 10 | +from openpyxl.styles.fills import GradientFill |
| 11 | +from openpyxl.styles.named_styles import NamedStyle |
| 12 | +from openpyxl.reader.excel import load_workbook |
| 13 | + |
| 14 | + |
| 15 | +def ready_to_excel(file_name): |
| 16 | + 'the read data from excel file' |
| 17 | + if file_name is None or len(file_name.strip()) == 0: |
| 18 | + raise ValueError('the current file name is invaild.') |
| 19 | + wb = load_workbook(file_name) |
| 20 | + sheet_names = wb.sheetnames |
| 21 | + # names = [] |
| 22 | + for name in sheet_names: |
| 23 | + print(name.encode('utf-8')) |
| 24 | + # names.append(name.encode('utf-8')) |
| 25 | + # print(names) |
| 26 | + |
| 27 | + |
| 28 | +def write_to_excel(file_name): |
| 29 | + 'the write data to excel file' |
| 30 | + if file_name is None or len(file_name.strip()) == 0: |
| 31 | + raise ValueError('the current file name is invaild.') |
| 32 | + |
| 33 | + wb = Workbook() # 工作簿 |
| 34 | + work_sheet_of_default = wb.active # 当前工作表,等价于wb.active(0) |
| 35 | + work_sheet_of_default.title = u"测试0" # 当前工作表title |
| 36 | + # 单元格赋值 |
| 37 | + work_sheet_of_default['A8'] = 'A8' |
| 38 | + ws_cell = work_sheet_of_default.cell(row=9, column=1, value='A9') |
| 39 | + ws_cell.value = 'A99' |
| 40 | + # 合并/撤销单元格 |
| 41 | + work_sheet_of_default.merge_cells('B2:C2') |
| 42 | + # work_sheet_of_default.merge_cells(start_row=2, |
| 43 | + # start_column=2, |
| 44 | + # end_row=2, |
| 45 | + # end_column=3) |
| 46 | + work_sheet_of_default['B2'] = u'合并的单元格' |
| 47 | + # work_sheet_of_default.unmerge_cells('B2:C2') |
| 48 | + # 区间访问 |
| 49 | + for row in range(1, 10): |
| 50 | + work_sheet_of_default.append(range(10)) |
| 51 | + |
| 52 | + # 创建新的工作表 WorkSheet |
| 53 | + work_sheet_of_test = wb.create_sheet() |
| 54 | + # work_sheet_of_test = wb.create_sheet(title='test', |
| 55 | + # index=1) # title 和index 可不写省略 |
| 56 | + work_sheet_of_test.title = u'test1' |
| 57 | + work_sheet_of_test.sheet_properties.tabColor = "1072BA" # 设置选项卡背景色 |
| 58 | + # 单元格取值 |
| 59 | + # for row in work_sheet_of_test.values(): |
| 60 | + # for value in row: |
| 61 | + # print(value) |
| 62 | + # 行列访问 |
| 63 | + row_3 = work_sheet_of_test['3'] |
| 64 | + row_range = work_sheet_of_test['3:5'] |
| 65 | + row_str = 'row: %s\t\t%s' % (row_3, row_range) |
| 66 | + row_str = 'row pass' |
| 67 | + print(row_str) |
| 68 | + column_c = work_sheet_of_test['C'] |
| 69 | + column_ragne = work_sheet_of_test['C:D'] |
| 70 | + column_str = 'column: %s\t\t%s' % (column_c, column_ragne) |
| 71 | + column_str = 'column pass' |
| 72 | + print(column_str) |
| 73 | + # 迭代访问,迭代行 |
| 74 | + for row in work_sheet_of_test.iter_rows(min_row=1, max_row=2, max_col=3): |
| 75 | + for cell in row: |
| 76 | + # print(cell) |
| 77 | + pass |
| 78 | + # 迭代访问,迭代列 |
| 79 | + for col in work_sheet_of_test.iter_cols(min_row=1, max_row=2, |
| 80 | + max_col=3): # 只读模式不可用 |
| 81 | + for cell in col: |
| 82 | + # print (cell) |
| 83 | + pass |
| 84 | + |
| 85 | + work_sheet_pi = wb.create_sheet(title=u"Pi") |
| 86 | + work_sheet_pi['F5'] = 3.14 |
| 87 | + # 日期格式 |
| 88 | + work_sheet_pi['A3'] = datetime.datetime.now() |
| 89 | + print(work_sheet_pi['A3'].number_format) |
| 90 | + # 公式 |
| 91 | + work_sheet_pi['B2'] = "=SUM(1, 1)" |
| 92 | + # 折叠行/列 |
| 93 | + work_sheet_pi.row_dimensions.group(10, 12, hidden=True) |
| 94 | + work_sheet_pi.column_dimensions.group('F', 'G', hidden=True) |
| 95 | + |
| 96 | + work_sheet_ascii = wb.create_sheet(title=u"字母") |
| 97 | + for row in range(10, 20): |
| 98 | + for col in range(10, 20): |
| 99 | + work_sheet_ascii.cell(column=col, |
| 100 | + row=row, |
| 101 | + value=str("%s%s" % (row, col))) |
| 102 | + # minimum_bounding_range = work_sheet_ascii.calculate_dimension() |
| 103 | + # print(minimum_bounding_range) |
| 104 | + work_sheet_style = wb.create_sheet(title=u"style") |
| 105 | + # |
| 106 | + # 样式默认值 |
| 107 | + # |
| 108 | + font = Font(name='Calibri', |
| 109 | + size=11, |
| 110 | + bold=False, |
| 111 | + italic=False, |
| 112 | + vertAlign=None, |
| 113 | + underline='none', |
| 114 | + strike=False, |
| 115 | + color='FF000000') |
| 116 | + fill = PatternFill(fill_type=None, |
| 117 | + start_color='FFFFFFFF', |
| 118 | + end_color='FF000000') |
| 119 | + # border = Border(left=Side(border_style=None, color='FF000000'), |
| 120 | + # right=Side(border_style=None, color='FF000000'), |
| 121 | + # top=Side(border_style=None, color='FF000000'), |
| 122 | + # bottom=Side(border_style=None, color='FF000000'), |
| 123 | + # diagonal=Side(border_style=None, color='FF000000'), |
| 124 | + # diagonal_direction=0, |
| 125 | + # outline=Side(border_style=None, color='FF000000'), |
| 126 | + # vertical=Side(border_style=None, color='FF000000'), |
| 127 | + # horizontal=Side(border_style=None, color='FF000000')) |
| 128 | + # alignment = Alignment(horizontal='general', |
| 129 | + # vertical='bottom', |
| 130 | + # text_rotation=0, |
| 131 | + # wrap_text=False, |
| 132 | + # shrink_to_fit=False, |
| 133 | + # indent=0) |
| 134 | + # number_format = 'General' |
| 135 | + # protection = Protection(locked=True, hidden=False) |
| 136 | + # |
| 137 | + # 单元格样式在对象之间共享,并且一旦分配它们便无法更改 |
| 138 | + # |
| 139 | + # https://openpyxl.readthedocs.io/en/2.6/styles.html#cell-styles-and-named-styles |
| 140 | + ws_b5 = work_sheet_style['B5'] |
| 141 | + font1 = copy(font) # 样式复制 |
| 142 | + font1.name = 'Arial' |
| 143 | + ws_b5.font = font1 |
| 144 | + ws_b5.value = 'ws_b5' |
| 145 | + |
| 146 | + ws_d6 = work_sheet_style['D6'] |
| 147 | + font2 = Font(name='Tahoma', color=colors.RED, italic=True, size=16) |
| 148 | + ws_d6.font = font2 |
| 149 | + ws_d6.value = 'ws_d6' |
| 150 | + # |
| 151 | + # 样式合并单元格 |
| 152 | + # |
| 153 | + work_sheet_style.merge_cells('B8:F10') |
| 154 | + top_left_cell = work_sheet_style['B8'] |
| 155 | + top_left_cell.value = u"样式合并单元格" |
| 156 | + # 边 |
| 157 | + thin = Side(border_style="thin", color="000000") |
| 158 | + double = Side(border_style="double", color="ff0000") |
| 159 | + # 边框 |
| 160 | + top_left_cell.border = Border(top=double, |
| 161 | + left=thin, |
| 162 | + right=thin, |
| 163 | + bottom=double) |
| 164 | + # 填充 |
| 165 | + top_left_cell.fill = PatternFill("solid", fgColor="DDDDDD") |
| 166 | + fill = GradientFill(stop=("000000", "FFFFFF")) |
| 167 | + top_left_cell.fill = fill |
| 168 | + top_left_cell.font = Font(b=True, color="FF0000") |
| 169 | + # 对齐模式 |
| 170 | + top_left_cell.alignment = Alignment(horizontal="center", vertical="center") |
| 171 | + |
| 172 | + # |
| 173 | + # 页面设置 |
| 174 | + # |
| 175 | + work_sheet_style.page_setup.orientation = work_sheet_style.ORIENTATION_LANDSCAPE |
| 176 | + work_sheet_style.page_setup.paperSize = work_sheet_style.PAPERSIZE_TABLOID |
| 177 | + work_sheet_style.page_setup.fitToWidth = 1 |
| 178 | + work_sheet_style.page_setup.fitToHeight = 0 |
| 179 | + |
| 180 | + # |
| 181 | + # 与单元样式相反,命名样式是可变的 |
| 182 | + # |
| 183 | + # 当您想一次将格式应用于许多不同的单元格时,它们很有意义。注意 将命名样式分配给单元后,对该样式的其他更改将 不会影响该单元。 |
| 184 | + # 一旦将命名样式注册到工作簿中,就可以简单地通过名称来引用它。 |
| 185 | + # |
| 186 | + |
| 187 | + # 创建样式,注册与指定 |
| 188 | + high_light_style = NamedStyle(name="highlight") |
| 189 | + high_light_style.font = Font(bold=True, size=20, color=colors.RED) |
| 190 | + boder_style = Side(style='thick', color="000000") |
| 191 | + high_light_style.border = Border(left=boder_style, |
| 192 | + top=boder_style, |
| 193 | + right=boder_style, |
| 194 | + bottom=boder_style) |
| 195 | + wb.add_named_style(high_light_style) # 工作簿注册样式,命名样式在首次分配给单元时也会自动注册 |
| 196 | + ws_g8 = work_sheet_style['G8'] |
| 197 | + ws_g8.style = high_light_style |
| 198 | + ws_g8.value = 'ws_g8' |
| 199 | + ws_h6 = work_sheet_style['H6'] |
| 200 | + ws_h6.style = 'highlight' # 工作簿注册样式后,仅使用名称指定样式 |
| 201 | + ws_h6.value = 'ws_h6' |
| 202 | + # |
| 203 | + # 使用内置样式 |
| 204 | + # 请参考: |
| 205 | + # https://openpyxl.readthedocs.io/en/2.6/styles.html#using-builtin-styles |
| 206 | + # |
| 207 | + |
| 208 | + wb.save(filename=file_name) |
| 209 | + |
| 210 | + |
| 211 | +if __name__ == "__main__": |
| 212 | + u'''读写Excel 文件''' |
| 213 | + file_name = generate_complex_or_fmt_file_name('excel', 'excel.xlsx') |
| 214 | + # |
| 215 | + # 写xlsx 文件 |
| 216 | + # |
| 217 | + write_to_excel(file_name) |
| 218 | + # |
| 219 | + # 读xlsx 文件 |
| 220 | + # |
| 221 | + # ready_to_excel(file_name) |
0 commit comments