From a010e52ceabf7390a0bed0e617f8e1eddc2fdd09 Mon Sep 17 00:00:00 2001 From: Jeff fu Date: Tue, 30 Dec 2025 15:17:06 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E8=A1=A8=E6=A0=BC=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E5=8A=9F=E8=83=BD=EF=BC=8C=E6=94=AF=E6=8C=81=E8=A1=A8?= =?UTF-8?q?=E5=A4=B4=E5=BA=95=E8=89=B2=E4=B8=8E=E9=9A=94=E8=A1=8C=E5=BA=95?= =?UTF-8?q?=E8=89=B2=EF=BC=8C=E8=B0=83=E6=95=B4=E5=8D=95=E5=85=83=E6=A0=BC?= =?UTF-8?q?=E5=AF=B9=E9=BD=90=E6=96=B9=E5=BC=8F=E4=BB=A5=E6=8F=90=E5=8D=87?= =?UTF-8?q?=E5=8F=AF=E8=AF=BB=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../actions/export_to_docx/export_to_word.py | 33 +++++++++++++++---- plugins/actions/export_to_docx/导出为Word.py | 33 +++++++++++++++---- 2 files changed, 52 insertions(+), 14 deletions(-) diff --git a/plugins/actions/export_to_docx/export_to_word.py b/plugins/actions/export_to_docx/export_to_word.py index f28de8b..4f0b2ca 100644 --- a/plugins/actions/export_to_docx/export_to_word.py +++ b/plugins/actions/export_to_docx/export_to_word.py @@ -669,10 +669,19 @@ class Action: run.font.size = Pt(10) def add_table(self, doc: Document, table_lines: List[str]): - """Add table""" + """Add table with header shading and zebra striping""" if len(table_lines) < 2: return + def _set_cell_shading(cell, fill: str): + tc_pr = cell._element.get_or_add_tcPr() + shd = OxmlElement("w:shd") + shd.set(qn("w:fill"), fill) + tc_pr.append(shd) + + header_fill = "F2F2F2" + zebra_fill = "FBFBFB" + # Parse table data rows = [] for line in table_lines: @@ -701,23 +710,33 @@ class Action: cell = row.cells[col_idx] # Clear default paragraph cell.paragraphs[0].clear() - self.add_formatted_text(cell.paragraphs[0], cell_text) + para = cell.paragraphs[0] + para.paragraph_format.space_after = Pt(3) + para.paragraph_format.space_before = Pt(1) + para.alignment = WD_ALIGN_PARAGRAPH.LEFT + + self.add_formatted_text(para, cell_text) # Set cell font - for run in cell.paragraphs[0].runs: + for run in para.runs: run.font.name = "Times New Roman" run._element.rPr.rFonts.set(qn("w:eastAsia"), "SimSun") run.font.size = Pt(10) - # Bold header + # Header bold + shading if row_idx == 0: - for run in cell.paragraphs[0].runs: + for run in para.runs: run.bold = True + _set_cell_shading(cell, header_fill) + # Zebra striping + elif row_idx % 2 == 1: + _set_cell_shading(cell, zebra_fill) - # Center align cells + # Left align all cells for readability for row in table.rows: for cell in row.cells: - cell.paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.CENTER + for para in cell.paragraphs: + para.alignment = WD_ALIGN_PARAGRAPH.LEFT def add_list_to_doc( self, doc: Document, items: List[Tuple[int, str]], list_type: str diff --git a/plugins/actions/export_to_docx/导出为Word.py b/plugins/actions/export_to_docx/导出为Word.py index 35fb248..eb3ad48 100644 --- a/plugins/actions/export_to_docx/导出为Word.py +++ b/plugins/actions/export_to_docx/导出为Word.py @@ -668,10 +668,19 @@ class Action: run.font.size = Pt(10) def add_table(self, doc: Document, table_lines: List[str]): - """添加表格""" + """添加表格,支持表头底色与隔行底色""" if len(table_lines) < 2: return + def _set_cell_shading(cell, fill: str): + tc_pr = cell._element.get_or_add_tcPr() + shd = OxmlElement("w:shd") + shd.set(qn("w:fill"), fill) + tc_pr.append(shd) + + header_fill = "F2F2F2" + zebra_fill = "FBFBFB" + # 解析表格数据 rows = [] for line in table_lines: @@ -700,23 +709,33 @@ class Action: cell = row.cells[col_idx] # 清除默认段落 cell.paragraphs[0].clear() - self.add_formatted_text(cell.paragraphs[0], cell_text) + para = cell.paragraphs[0] + para.paragraph_format.space_after = Pt(3) + para.paragraph_format.space_before = Pt(1) + para.alignment = WD_ALIGN_PARAGRAPH.LEFT + + self.add_formatted_text(para, cell_text) # 设置单元格字体 - for run in cell.paragraphs[0].runs: + for run in para.runs: run.font.name = "Times New Roman" run._element.rPr.rFonts.set(qn("w:eastAsia"), "宋体") run.font.size = Pt(10) - # 表头加粗 + # 表头加粗并填充底色 if row_idx == 0: - for run in cell.paragraphs[0].runs: + for run in para.runs: run.bold = True + _set_cell_shading(cell, header_fill) + # 隔行底色 + elif row_idx % 2 == 1: + _set_cell_shading(cell, zebra_fill) - # 设置表格列宽度自适应 + # 统一列对齐为左对齐,避免居中导致阅读困难 for row in table.rows: for cell in row.cells: - cell.paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.CENTER + for para in cell.paragraphs: + para.alignment = WD_ALIGN_PARAGRAPH.LEFT def add_list_to_doc( self, doc: Document, items: List[Tuple[int, str]], list_type: str