feat: support full-cell markdown italic formatting in excel export

This commit is contained in:
fujie
2026-01-03 14:18:32 +08:00
parent 3e73fcb3f0
commit 4e59bb6518
2 changed files with 52 additions and 8 deletions

View File

@@ -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): for i, table in enumerate(tables):
try: try:
table_data = table["data"] table_data = table["data"]
@@ -832,6 +843,7 @@ class Action:
date_format, date_format,
sequence_format, sequence_format,
text_bold_format, text_bold_format,
text_italic_format,
) )
except Exception as e: except Exception as e:
@@ -856,6 +868,7 @@ class Action:
date_format, date_format,
sequence_format, sequence_format,
text_bold_format=None, text_bold_format=None,
text_italic_format=None,
): ):
""" """
Apply enhanced formatting Apply enhanced formatting
@@ -864,7 +877,7 @@ class Action:
- Text: Left aligned - Text: Left aligned
- Date: Center aligned - Date: Center aligned
- Sequence: Center aligned - Sequence: Center aligned
- Supports full cell Markdown bold (**text**) - Supports full cell Markdown bold (**text**) and italic (*text*)
""" """
try: try:
# 1. Write headers (Center aligned) # 1. Write headers (Center aligned)
@@ -928,13 +941,22 @@ class Action:
if content_type == "text" and isinstance(value, str): if content_type == "text" and isinstance(value, str):
# Check for full cell bold (**text**) # Check for full cell bold (**text**)
match = re.fullmatch(r"\*\*(.+)\*\*", value.strip()) match_bold = re.fullmatch(r"\*\*(.+)\*\*", value.strip())
if match: # Check for full cell italic (*text*)
match_italic = re.fullmatch(r"\*(.+)\*", value.strip())
if match_bold:
# Extract content and apply bold format # Extract content and apply bold format
clean_value = match.group(1) clean_value = match_bold.group(1)
worksheet.write( worksheet.write(
row_idx + 1, col_idx, clean_value, text_bold_format 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: else:
worksheet.write(row_idx + 1, col_idx, value, current_format) worksheet.write(row_idx + 1, col_idx, value, current_format)
else: else:

View File

@@ -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): for i, table in enumerate(tables):
try: try:
table_data = table["data"] table_data = table["data"]
@@ -837,6 +848,7 @@ class Action:
date_format, date_format,
sequence_format, sequence_format,
text_bold_format, text_bold_format,
text_italic_format,
) )
except Exception as e: except Exception as e:
@@ -861,6 +873,7 @@ class Action:
date_format, date_format,
sequence_format, sequence_format,
text_bold_format=None, text_bold_format=None,
text_italic_format=None,
): ):
""" """
应用符合中国官方表格规范的格式化 应用符合中国官方表格规范的格式化
@@ -869,7 +882,7 @@ class Action:
- 文本: 左对齐 - 文本: 左对齐
- 日期: 居中对齐 - 日期: 居中对齐
- 序号: 居中对齐 - 序号: 居中对齐
- 支持全单元格 Markdown 粗体 (**text**) - 支持全单元格 Markdown 粗体 (**text**) 和斜体 (*text*)
""" """
try: try:
# 1. 写入表头(居中对齐) # 1. 写入表头(居中对齐)
@@ -933,13 +946,22 @@ class Action:
if content_type == "text" and isinstance(value, str): if content_type == "text" and isinstance(value, str):
# 检查是否全单元格加粗 (**text**) # 检查是否全单元格加粗 (**text**)
match = re.fullmatch(r"\*\*(.+)\*\*", value.strip()) match_bold = re.fullmatch(r"\*\*(.+)\*\*", value.strip())
if match: # 检查是否全单元格斜体 (*text*)
match_italic = re.fullmatch(r"\*(.+)\*", value.strip())
if match_bold:
# 提取内容并应用粗体格式 # 提取内容并应用粗体格式
clean_value = match.group(1) clean_value = match_bold.group(1)
worksheet.write( worksheet.write(
row_idx + 1, col_idx, clean_value, text_bold_format 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: else:
worksheet.write(row_idx + 1, col_idx, value, current_format) worksheet.write(row_idx + 1, col_idx, value, current_format)
else: else: