python-openpyxl (style design)

from openpyxl.styles import PatternFill, Border, Side, Alignment, Protection, Font
from openpyxl import Workbook
from openpyxl.utils.dataframe import dataframe_to_rows
wb = Workbook()
        ws = wb.active
        # 对索引重命名,以保证后续index为FALSE时保留
        cc.index.name = 'code1'
        # 按行写入Excel,reset_index()重置索引。
        for i in dataframe_to_rows(cc.reset_index(), index=False, header=False):
            ws.append(i)

The result shows 1:
insert image description here

wb = Workbook()
        ws = wb.active
        # 对索引重命名,以保证后续index为FALSE时保留
        cc.index.name = 'code1'
        # 按行写入Excel,reset_index()重置索引。
        for i in dataframe_to_rows(cc.reset_index(), index=False, header=False):
            ws.append(i)

        font = Font(name='微软雅黑',
                    size=12,
                    bold=False,
                    italic=False,
                    vertAlign=None,  # 纵向对齐方式(有三种:baseline,superscript, subscript)
                    underline='none',
                    strike=False,  # 删除线
                    color='FF000000')
        # 设置填充样式
        fill = PatternFill(fill_type=None,  # 填充样式
                           start_color='FFFFFFFF',
                           end_color='FF000000')
        # 边框设置
        border = Border(left=Side(border_style='thin',
                                  color='FF000000'),
                        right=Side(border_style='thin',
                                   color='FF000000'),
                        top=Side(border_style='thin',
                                 color='FF000000'),
                        bottom=Side(border_style='thin',
                                    color='FF000000')
                        )
        # 对齐方式
        alignment = Alignment(horizontal='center',
                              # 水平对齐模式(可以左对齐left,还有居中center和右对齐right,分散对齐distributed,跨列居中centerContinuous,两端对齐justify,填充fill,常规general)
                              vertical='center',  # 垂直对齐模式(可以居中center,还可以靠上top,靠下bottom,两端对齐justify,分散对齐distributed)
                              text_rotation=0,  # 旋转角度
                              wrap_text=False)  # 是否自动换行
        # 调整列宽,调整行宽(.height)
        ws.column_dimensions["A"].width = 15
        ws.column_dimensions["B"].width = 35
        ws.column_dimensions["C"].width = 15
        ws.column_dimensions["D"].width = 15
        ws.column_dimensions["E"].width = 15
        ws.column_dimensions["F"].width = 15
        ws.column_dimensions["G"].width = 15
        # ws.max_row获得表格的最大行数,取得遍历次数,使用for循环遍历
        print(ws.max_column, ws.min_column, ws.max_row, ws.min_row)
        for k in range(1, ws.max_row + 1):
            # 调整行高
            ws.row_dimensions[k].height = 20

        for i in range(1, ws.max_column + 1):
            for j in range(1, ws.max_row + 1):
                ws.cell(j, i).alignment = alignment
                ws.cell(j, i).font = font

        for i in range(1, ws.max_column + 1):
            for j in range(1, len(cc)+1):
                ws.cell(j, i).border = border

        wb.save(self.tar_f)

The result shows 2:
insert image description here

Guess you like

Origin blog.csdn.net/weixin_44964850/article/details/122438140