function format_inline_references
Formats inline citation references (e.g., [1], [2]) in a Word document paragraph by applying italic styling to them while preserving the rest of the text.
/tf/active/vicechatdev/enhanced_word_converter_fixed.py
480 - 502
simple
Purpose
This function is designed to enhance the visual formatting of academic or technical documents by automatically italicizing numerical references in square brackets within a paragraph. It uses regex pattern matching to identify references, clears the paragraph's existing runs, and rebuilds the paragraph with proper formatting applied to reference citations while keeping regular text unchanged. This is particularly useful for automated document generation where consistent reference styling is required.
Source Code
def format_inline_references(paragraph):
"""Format inline references [1], [2], etc. to be italic in a paragraph"""
# Get the full text of the paragraph
full_text = paragraph.text
# Check if there are any references to format
if not re.search(r'\[\d+\]', full_text):
return
# Clear all runs in the paragraph
paragraph.clear()
# Split the text around references and rebuild with proper formatting
parts = re.split(r'(\[\d+\])', full_text)
for part in parts:
if re.match(r'\[\d+\]', part):
# This is a reference - make it italic
run = paragraph.add_run(part)
run.italic = True
elif part: # Only add non-empty parts
# Regular text
paragraph.add_run(part)
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
paragraph |
- | - | positional_or_keyword |
Parameter Details
paragraph: A python-docx Paragraph object representing a paragraph in a Word document. This object must have the 'text', 'clear()', and 'add_run()' methods available. The paragraph's content will be modified in-place to apply italic formatting to any inline references matching the pattern [digit(s)].
Return Value
Returns None (implicit). The function modifies the input paragraph object in-place by clearing its existing runs and rebuilding them with appropriate formatting. If no references matching the pattern [\d+] are found, the function returns early without making any changes.
Dependencies
repython-docx
Required Imports
import re
from docx import Document
Usage Example
from docx import Document
import re
# Assuming the format_inline_references function is defined
# Create or open a Word document
doc = Document()
# Add a paragraph with inline references
para = doc.add_paragraph('This is a statement [1] with multiple references [2] and more text [3].')
# Format the inline references to be italic
format_inline_references(para)
# Save the document
doc.save('formatted_document.docx')
# The references [1], [2], and [3] will now be italicized in the saved document
Best Practices
- Ensure the paragraph object is a valid python-docx Paragraph instance before calling this function
- Be aware that this function modifies the paragraph in-place and clears all existing formatting (not just for references)
- The function only formats references in the pattern [number] - other reference styles (e.g., (1), [Author2020]) will not be affected
- If the paragraph has complex formatting (bold, colors, fonts) on non-reference text, that formatting will be lost as the paragraph is cleared and rebuilt
- Consider backing up the original paragraph content if you need to preserve other formatting attributes
- The function performs an early return if no references are found, making it safe to call on all paragraphs without performance concerns
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function add_inline_formatting_to_paragraph 73.2% similar
-
function add_inline_formatting_to_paragraph_v1 68.7% similar
-
function add_markdown_formatting_to_paragraph 63.4% similar
-
function format_inline_markdown 58.0% similar
-
function add_hyperlink_to_paragraph 57.5% similar