function add_inline_formatting_to_paragraph
Parses markdown-formatted text and applies inline formatting (bold, italic, code) to a Microsoft Word paragraph object using the python-docx library.
/tf/active/vicechatdev/vice_ai/complex_app.py
1578 - 1606
moderate
Purpose
This function enables conversion of markdown-style inline formatting to native Word document formatting. It processes text containing markdown syntax for bold (**text**), italic (*text*), and inline code (`text`) and applies the corresponding formatting to runs within a Word paragraph. This is useful when generating Word documents from markdown content while preserving text styling.
Source Code
def add_inline_formatting_to_paragraph(paragraph, text):
"""Add text with inline markdown formatting to a Word paragraph"""
import re
# Handle bold text **text**
parts = re.split(r'\*\*(.*?)\*\*', text)
for i, part in enumerate(parts):
if i % 2 == 0: # Regular text
if part:
# Check for italic within regular text
italic_parts = re.split(r'\*(.*?)\*', part)
for j, italic_part in enumerate(italic_parts):
if j % 2 == 0: # Regular text
if italic_part:
# Check for code within regular text
code_parts = re.split(r'`(.*?)`', italic_part)
for k, code_part in enumerate(code_parts):
if k % 2 == 0: # Regular text
if code_part:
paragraph.add_run(code_part)
else: # Code text
run = paragraph.add_run(code_part)
run.font.name = 'Courier New'
else: # Italic text
run = paragraph.add_run(italic_part)
run.italic = True
else: # Bold text
run = paragraph.add_run(part)
run.bold = True
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
paragraph |
- | - | positional_or_keyword |
text |
- | - | positional_or_keyword |
Parameter Details
paragraph: A python-docx Paragraph object to which formatted text runs will be added. This should be an existing paragraph from a Document object created using the python-docx library.
text: A string containing markdown-formatted text with inline formatting markers. Supports bold (**text**), italic (*text*), and inline code (`text`) syntax. Can contain nested formatting within bold sections.
Return Value
This function returns None (implicitly). It modifies the paragraph object in-place by adding formatted text runs to it.
Dependencies
python-docxre
Required Imports
import re
from docx import Document
Usage Example
from docx import Document
import re
def add_inline_formatting_to_paragraph(paragraph, text):
import re
parts = re.split(r'\*\*(.*?)\*\*', text)
for i, part in enumerate(parts):
if i % 2 == 0:
if part:
italic_parts = re.split(r'\*(.*?)\*', part)
for j, italic_part in enumerate(italic_parts):
if j % 2 == 0:
if italic_part:
code_parts = re.split(r'`(.*?)`', italic_part)
for k, code_part in enumerate(code_parts):
if k % 2 == 0:
if code_part:
paragraph.add_run(code_part)
else:
run = paragraph.add_run(code_part)
run.font.name = 'Courier New'
else:
run = paragraph.add_run(italic_part)
run.italic = True
else:
run = paragraph.add_run(part)
run.bold = True
# Create a Word document
doc = Document()
paragraph = doc.add_paragraph()
# Add formatted text
text = "This is **bold text**, this is *italic text*, and this is `code text`."
add_inline_formatting_to_paragraph(paragraph, text)
# Save the document
doc.save('formatted_document.docx')
Best Practices
- Ensure the paragraph parameter is a valid python-docx Paragraph object before calling this function
- The function uses nested regex splitting which processes formatting in order: bold first, then italic within non-bold sections, then code within non-italic sections
- Nested formatting is limited: italic and code can appear within regular text sections, but not within bold sections
- The function does not handle escaped markdown characters (e.g., \*\*) - literal asterisks or backticks will be processed as formatting markers
- Code text is rendered in 'Courier New' font - ensure this font is available in the target environment
- The function modifies the paragraph object in-place and does not return a value
- For complex nested formatting or more advanced markdown features, consider using a dedicated markdown-to-docx library
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function add_markdown_formatting_to_paragraph 89.5% similar
-
function add_inline_formatting_to_paragraph_v1 87.0% similar
-
function add_formatted_content_to_word 78.4% similar
-
function add_formatted_content_to_word_v1 74.0% similar
-
function format_inline_references 73.2% similar