天天看点

python-docx 垂直居中以及行高失效问题

python-docx 的垂直居中问题以及行高失效问题

我翻了下源码发现这个包暂时没有办法全局Table设置垂直居中,只找到了cell垂直居中的方法。所以我修改了行内的方法在写之前先设置样式。行高失效的话是因为这个模块的缺陷,导致直接对新的row赋值高度失效,所以就只能使用

self.table.rows[当前行索引].height

来设置行高。下边是我对table相关操作简单封装的一个模块。
封装之后如下
class CreateTable:
    """
        table 操作
    """

    def __init__(self, rows, cols, width_list, height, document, font_size=10, font_color=(0, 0, 0), floats='LEFT',
                 font_name=u'仿宋'):
        """
        添加rows行cols列表格
        :param rows: 行
        :param cols: 列
        :param font_size: 字体大小
        :param font_color: 字体颜色
        :param floats: 居中格式
        """
        self.rows = rows
        self.cols = cols
        self.font_name = font_name
        self.width_list = width_list
        self.height = height
        self.now_row = 0
        self.table = document.add_table(rows=rows, cols=cols, style='Table Grid')
        self.table.style.font.size = Pt(font_size)
        self.table.style.font.name = font_name
        self.table.style.element.rPr.rFonts.set(qn('w:eastAsia'), font_name)
        self.table.style.font.color.rgb = RGBColor(*font_color)
        self.table.style.paragraph_format.alignment = getattr(WD_PARAGRAPH_ALIGNMENT, floats)
        self.table.alignment = WD_TABLE_ALIGNMENT.CENTER
        self.set_row_width()

    def set_row_width(self):
        for i in range(self.cols):
            self.table.cell(0, i).width = Cm(self.width_list[i])
        for i in range(self.rows):
            self.table.rows[i].height = Cm(self.height)

    def merge(self, tuple1, tuple2, text, font_name=u'仿宋', font_size=10, bold=False):
        self.table.cell(*tuple1).merge(self.table.cell(*tuple2))
        tr = self.table.cell(*tuple1).paragraphs[0].add_run(text)
        tr.font.name = font_name
        tr.font.size = Pt(font_size)
        tr.font.bold = bold
        xu = self.table.cell(*tuple1)
        xu.vertical_alignment = WD_CELL_VERTICAL_ALIGNMENT.CENTER
        xu.alignment = WD_TABLE_ALIGNMENT
        xt, y = tuple1
        self.table.rows[xt].height = Cm(self.height)

    def write_row(self, tuple1, tuple2, data_list, font_size=11, bold=False):
        LA, LB = tuple1
        RA, RB = tuple2
        if (LA == RA and RB >= LB) or (LB == RB and RA >= LA):
            if LA == RA:
                for index, i in enumerate(range(LB, RB)):
                    self._write((LA, i), data_list[index], font_size, bold)
                return
            if LB == RB:
                for index, i in enumerate(range(LA, RA)):
                    self._write((i, LB), data_list[index], font_size, bold)
                return

    def __add_row(self):
        self.now_row += 1
        return self.table.add_row().cells

    def add_new_row(self):
        self.now_row += 1
        return self.table.add_row().cells

    def add_row(self, data_list, color=None, font_size=11, bold=False):
        """
        :param bold:
        :param font_size:
        :param data_list: 数据列表
        :param color: 字体下标和颜色组成的字典 ps: {0:'RED',2:'YELLOW}
        :return:
        """
        row_cells = self.__add_row()
        # 行高设置在这里
        self.table.rows[self.now_row].height = Cm(self.height)
        for i in range(self.cols):
            xa = row_cells[i]
            xa.width = Cm(self.width_list[i])
            # 垂直居中在这里
            xa.vertical_alignment = WD_CELL_VERTICAL_ALIGNMENT.CENTER
            xx = xa.paragraphs[0].add_run(str(data_list[i]))
            xx.font.name = self.font_name
            xx.font.size = Pt(font_size)
            xx.font.bold = bold
            if color and i in color.keys():
                xx.font.highlight_color = getattr(WD_COLOR_INDEX, color[i])

    def _write(self, tuple1, text, font_size, bold):
        xu = self.table.cell(*tuple1)
        xu.vertical_alignment = WD_CELL_VERTICAL_ALIGNMENT.CENTER
        xx = xu.paragraphs[0].add_run(text)
        xx.font.name = self.font_name
        xx.font.size = Pt(font_size)
        xx.font.bold = bold
           
  • PS : 其中

    xu.vertical_alignment = WD_CELL_VERTICAL_ALIGNMENT.CENTER

    xu

    为当前需要填充的cell,

    vertical_alignment = WD_CELL_VERTICAL_ALIGNMENT.CENTER

    即为设置垂直居中的方法。