feat: support markdown bold formatting in excel export
This commit is contained in:
@@ -749,6 +749,9 @@ class Action:
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Bold font format (for rich text)
|
||||||
|
bold_font_format = workbook.add_format({"bold": True})
|
||||||
|
|
||||||
for i, table in enumerate(tables):
|
for i, table in enumerate(tables):
|
||||||
try:
|
try:
|
||||||
table_data = table["data"]
|
table_data = table["data"]
|
||||||
@@ -820,6 +823,7 @@ class Action:
|
|||||||
decimal_format,
|
decimal_format,
|
||||||
date_format,
|
date_format,
|
||||||
sequence_format,
|
sequence_format,
|
||||||
|
bold_font_format,
|
||||||
)
|
)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@@ -843,6 +847,7 @@ class Action:
|
|||||||
decimal_format,
|
decimal_format,
|
||||||
date_format,
|
date_format,
|
||||||
sequence_format,
|
sequence_format,
|
||||||
|
bold_font_format=None,
|
||||||
):
|
):
|
||||||
"""
|
"""
|
||||||
Apply enhanced formatting
|
Apply enhanced formatting
|
||||||
@@ -851,6 +856,7 @@ class Action:
|
|||||||
- Text: Left aligned
|
- Text: Left aligned
|
||||||
- Date: Center aligned
|
- Date: Center aligned
|
||||||
- Sequence: Center aligned
|
- Sequence: Center aligned
|
||||||
|
- Supports Markdown bold (**text**)
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
# 1. Write headers (Center aligned)
|
# 1. Write headers (Center aligned)
|
||||||
@@ -912,7 +918,22 @@ class Action:
|
|||||||
# Text - Left aligned
|
# Text - Left aligned
|
||||||
current_format = text_format
|
current_format = text_format
|
||||||
|
|
||||||
worksheet.write(row_idx + 1, col_idx, value, current_format)
|
if (
|
||||||
|
content_type == "text"
|
||||||
|
and isinstance(value, str)
|
||||||
|
and "**" in value
|
||||||
|
):
|
||||||
|
# Try to parse Markdown bold
|
||||||
|
self.write_rich_string_cell(
|
||||||
|
worksheet,
|
||||||
|
row_idx + 1,
|
||||||
|
col_idx,
|
||||||
|
value,
|
||||||
|
current_format,
|
||||||
|
bold_font_format,
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
worksheet.write(row_idx + 1, col_idx, value, current_format)
|
||||||
|
|
||||||
# 4. Auto-adjust column width
|
# 4. Auto-adjust column width
|
||||||
for col_idx, column in enumerate(headers):
|
for col_idx, column in enumerate(headers):
|
||||||
@@ -1002,3 +1023,49 @@ class Action:
|
|||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Error in basic formatting: {str(e)}")
|
print(f"Error in basic formatting: {str(e)}")
|
||||||
|
|
||||||
|
def write_rich_string_cell(
|
||||||
|
self, worksheet, row, col, text, cell_format, bold_format
|
||||||
|
):
|
||||||
|
"""
|
||||||
|
Parse Markdown bold and write to rich string cell
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
parts = []
|
||||||
|
current_text = text
|
||||||
|
has_bold = False
|
||||||
|
|
||||||
|
# Simple parsing logic: split by **
|
||||||
|
while "**" in current_text:
|
||||||
|
start = current_text.find("**")
|
||||||
|
end = current_text.find("**", start + 2)
|
||||||
|
|
||||||
|
if end != -1:
|
||||||
|
has_bold = True
|
||||||
|
# Add preceding normal text
|
||||||
|
if start > 0:
|
||||||
|
parts.append(current_text[:start])
|
||||||
|
|
||||||
|
# Add bold text
|
||||||
|
bold_text = current_text[start + 2 : end]
|
||||||
|
parts.append(bold_format)
|
||||||
|
parts.append(bold_text)
|
||||||
|
|
||||||
|
current_text = current_text[end + 2 :]
|
||||||
|
else:
|
||||||
|
break
|
||||||
|
|
||||||
|
# Add remaining text
|
||||||
|
if current_text:
|
||||||
|
parts.append(current_text)
|
||||||
|
|
||||||
|
if has_bold and len(parts) > 1:
|
||||||
|
# Must end with cell_format
|
||||||
|
parts.append(cell_format)
|
||||||
|
worksheet.write_rich_string(row, col, *parts)
|
||||||
|
else:
|
||||||
|
worksheet.write(row, col, text, cell_format)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error writing rich string: {e}")
|
||||||
|
worksheet.write(row, col, text, cell_format)
|
||||||
|
|||||||
@@ -754,6 +754,9 @@ class Action:
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# 粗体字体格式 (用于富文本)
|
||||||
|
bold_font_format = workbook.add_format({"bold": True})
|
||||||
|
|
||||||
for i, table in enumerate(tables):
|
for i, table in enumerate(tables):
|
||||||
try:
|
try:
|
||||||
table_data = table["data"]
|
table_data = table["data"]
|
||||||
@@ -825,6 +828,7 @@ class Action:
|
|||||||
decimal_format,
|
decimal_format,
|
||||||
date_format,
|
date_format,
|
||||||
sequence_format,
|
sequence_format,
|
||||||
|
bold_font_format,
|
||||||
)
|
)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@@ -848,6 +852,7 @@ class Action:
|
|||||||
decimal_format,
|
decimal_format,
|
||||||
date_format,
|
date_format,
|
||||||
sequence_format,
|
sequence_format,
|
||||||
|
bold_font_format=None,
|
||||||
):
|
):
|
||||||
"""
|
"""
|
||||||
应用符合中国官方表格规范的格式化
|
应用符合中国官方表格规范的格式化
|
||||||
@@ -856,6 +861,7 @@ class Action:
|
|||||||
- 文本: 左对齐
|
- 文本: 左对齐
|
||||||
- 日期: 居中对齐
|
- 日期: 居中对齐
|
||||||
- 序号: 居中对齐
|
- 序号: 居中对齐
|
||||||
|
- 支持 Markdown 粗体 (**text**)
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
# 1. 写入表头(居中对齐)
|
# 1. 写入表头(居中对齐)
|
||||||
@@ -917,7 +923,22 @@ class Action:
|
|||||||
# 文本类型 - 左对齐
|
# 文本类型 - 左对齐
|
||||||
current_format = text_format
|
current_format = text_format
|
||||||
|
|
||||||
worksheet.write(row_idx + 1, col_idx, value, current_format)
|
if (
|
||||||
|
content_type == "text"
|
||||||
|
and isinstance(value, str)
|
||||||
|
and "**" in value
|
||||||
|
):
|
||||||
|
# 尝试解析 Markdown 粗体
|
||||||
|
self.write_rich_string_cell(
|
||||||
|
worksheet,
|
||||||
|
row_idx + 1,
|
||||||
|
col_idx,
|
||||||
|
value,
|
||||||
|
current_format,
|
||||||
|
bold_font_format,
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
worksheet.write(row_idx + 1, col_idx, value, current_format)
|
||||||
|
|
||||||
# 4. 自动调整列宽
|
# 4. 自动调整列宽
|
||||||
for col_idx, column in enumerate(headers):
|
for col_idx, column in enumerate(headers):
|
||||||
@@ -1017,3 +1038,49 @@ class Action:
|
|||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Warning: Even basic formatting failed: {str(e)}")
|
print(f"Warning: Even basic formatting failed: {str(e)}")
|
||||||
|
|
||||||
|
def write_rich_string_cell(
|
||||||
|
self, worksheet, row, col, text, cell_format, bold_format
|
||||||
|
):
|
||||||
|
"""
|
||||||
|
解析 Markdown 粗体并写入富文本单元格
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
parts = []
|
||||||
|
current_text = text
|
||||||
|
has_bold = False
|
||||||
|
|
||||||
|
# 简单的解析逻辑:分割 **
|
||||||
|
while "**" in current_text:
|
||||||
|
start = current_text.find("**")
|
||||||
|
end = current_text.find("**", start + 2)
|
||||||
|
|
||||||
|
if end != -1:
|
||||||
|
has_bold = True
|
||||||
|
# 添加前面的普通文本
|
||||||
|
if start > 0:
|
||||||
|
parts.append(current_text[:start])
|
||||||
|
|
||||||
|
# 添加粗体文本
|
||||||
|
bold_text = current_text[start + 2 : end]
|
||||||
|
parts.append(bold_format)
|
||||||
|
parts.append(bold_text)
|
||||||
|
|
||||||
|
current_text = current_text[end + 2 :]
|
||||||
|
else:
|
||||||
|
break
|
||||||
|
|
||||||
|
# 添加剩余文本
|
||||||
|
if current_text:
|
||||||
|
parts.append(current_text)
|
||||||
|
|
||||||
|
if has_bold and len(parts) > 1:
|
||||||
|
# 必须以 cell_format 结尾
|
||||||
|
parts.append(cell_format)
|
||||||
|
worksheet.write_rich_string(row, col, *parts)
|
||||||
|
else:
|
||||||
|
worksheet.write(row, col, text, cell_format)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error writing rich string: {e}")
|
||||||
|
worksheet.write(row, col, text, cell_format)
|
||||||
|
|||||||
Reference in New Issue
Block a user