Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 20 additions & 13 deletions word_document_server/tools/document_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -633,8 +633,8 @@ async def merge_documents(target_filename: str, source_filenames: List[str], add

# Copy all paragraphs
for paragraph in source_doc.paragraphs:
# Create a new paragraph with the same text and style
new_paragraph = target_doc.add_paragraph(paragraph.text)
# Create a new empty paragraph and replicate style
new_paragraph = target_doc.add_paragraph()
new_paragraph.style = target_doc.styles['Normal'] # Default style

# Try to match the style if possible
Expand All @@ -644,17 +644,24 @@ async def merge_documents(target_filename: str, source_filenames: List[str], add
except:
pass

# Copy run formatting
for i, run in enumerate(paragraph.runs):
if i < len(new_paragraph.runs):
new_run = new_paragraph.runs[i]
# Copy basic formatting
new_run.bold = run.bold
new_run.italic = run.italic
new_run.underline = run.underline
# Font size if specified
if run.font.size:
new_run.font.size = run.font.size
# Copy each run individually to preserve formatting
for run in paragraph.runs:
new_run = new_paragraph.add_run(run.text)
new_run.bold = run.bold
new_run.italic = run.italic
new_run.underline = run.underline
try:
if run.font.name:
new_run.font.name = run.font.name
except Exception:
pass
if run.font.size:
new_run.font.size = run.font.size
try:
if run.font.color.rgb:
new_run.font.color.rgb = run.font.color.rgb
except Exception:
pass

# Copy all tables
for table in source_doc.tables:
Expand Down