From 4e59bb6518a766c4fb45394e901540e4ddc24d2f Mon Sep 17 00:00:00 2001 From: fujie Date: Sat, 3 Jan 2026 14:18:32 +0800 Subject: [PATCH] feat: support full-cell markdown italic formatting in excel export --- .../export_to_excel/export_to_excel.py | 30 ++++++++++++++++--- .../actions/export_to_excel/导出为Excel.py | 30 ++++++++++++++++--- 2 files changed, 52 insertions(+), 8 deletions(-) diff --git a/plugins/actions/export_to_excel/export_to_excel.py b/plugins/actions/export_to_excel/export_to_excel.py index 8a1e1f4..66d6f73 100644 --- a/plugins/actions/export_to_excel/export_to_excel.py +++ b/plugins/actions/export_to_excel/export_to_excel.py @@ -760,6 +760,17 @@ class Action: } ) + # Italic cell style (for full cell italics) + text_italic_format = workbook.add_format( + { + "border": 1, + "align": "left", + "valign": "vcenter", + "text_wrap": True, + "italic": True, + } + ) + for i, table in enumerate(tables): try: table_data = table["data"] @@ -832,6 +843,7 @@ class Action: date_format, sequence_format, text_bold_format, + text_italic_format, ) except Exception as e: @@ -856,6 +868,7 @@ class Action: date_format, sequence_format, text_bold_format=None, + text_italic_format=None, ): """ Apply enhanced formatting @@ -864,7 +877,7 @@ class Action: - Text: Left aligned - Date: Center aligned - Sequence: Center aligned - - Supports full cell Markdown bold (**text**) + - Supports full cell Markdown bold (**text**) and italic (*text*) """ try: # 1. Write headers (Center aligned) @@ -928,13 +941,22 @@ class Action: if content_type == "text" and isinstance(value, str): # Check for full cell bold (**text**) - match = re.fullmatch(r"\*\*(.+)\*\*", value.strip()) - if match: + match_bold = re.fullmatch(r"\*\*(.+)\*\*", value.strip()) + # Check for full cell italic (*text*) + match_italic = re.fullmatch(r"\*(.+)\*", value.strip()) + + if match_bold: # Extract content and apply bold format - clean_value = match.group(1) + clean_value = match_bold.group(1) worksheet.write( row_idx + 1, col_idx, clean_value, text_bold_format ) + elif match_italic: + # Extract content and apply italic format + clean_value = match_italic.group(1) + worksheet.write( + row_idx + 1, col_idx, clean_value, text_italic_format + ) else: worksheet.write(row_idx + 1, col_idx, value, current_format) else: diff --git a/plugins/actions/export_to_excel/导出为Excel.py b/plugins/actions/export_to_excel/导出为Excel.py index d02cbb9..8d422de 100644 --- a/plugins/actions/export_to_excel/导出为Excel.py +++ b/plugins/actions/export_to_excel/导出为Excel.py @@ -765,6 +765,17 @@ class Action: } ) + # 斜体单元格样式 (用于全单元格斜体) + text_italic_format = workbook.add_format( + { + "border": 1, + "align": "left", + "valign": "vcenter", + "text_wrap": True, + "italic": True, + } + ) + for i, table in enumerate(tables): try: table_data = table["data"] @@ -837,6 +848,7 @@ class Action: date_format, sequence_format, text_bold_format, + text_italic_format, ) except Exception as e: @@ -861,6 +873,7 @@ class Action: date_format, sequence_format, text_bold_format=None, + text_italic_format=None, ): """ 应用符合中国官方表格规范的格式化 @@ -869,7 +882,7 @@ class Action: - 文本: 左对齐 - 日期: 居中对齐 - 序号: 居中对齐 - - 支持全单元格 Markdown 粗体 (**text**) + - 支持全单元格 Markdown 粗体 (**text**) 和斜体 (*text*) """ try: # 1. 写入表头(居中对齐) @@ -933,13 +946,22 @@ class Action: if content_type == "text" and isinstance(value, str): # 检查是否全单元格加粗 (**text**) - match = re.fullmatch(r"\*\*(.+)\*\*", value.strip()) - if match: + match_bold = re.fullmatch(r"\*\*(.+)\*\*", value.strip()) + # 检查是否全单元格斜体 (*text*) + match_italic = re.fullmatch(r"\*(.+)\*", value.strip()) + + if match_bold: # 提取内容并应用粗体格式 - clean_value = match.group(1) + clean_value = match_bold.group(1) worksheet.write( row_idx + 1, col_idx, clean_value, text_bold_format ) + elif match_italic: + # 提取内容并应用斜体格式 + clean_value = match_italic.group(1) + worksheet.write( + row_idx + 1, col_idx, clean_value, text_italic_format + ) else: worksheet.write(row_idx + 1, col_idx, value, current_format) else: