Spaces:
Running
Running
Update updated_word.py
Browse files- updated_word.py +35 -20
updated_word.py
CHANGED
|
@@ -677,44 +677,59 @@ def replace_red_text_with_line_breaks(cell, attendance_list):
|
|
| 677 |
"""Custom function to replace red text with properly formatted attendance list"""
|
| 678 |
replacements_made = 0
|
| 679 |
|
| 680 |
-
# Find all red text runs
|
| 681 |
red_runs = []
|
|
|
|
|
|
|
| 682 |
for paragraph in cell.paragraphs:
|
| 683 |
for run in paragraph.runs:
|
| 684 |
if is_red(run) and run.text.strip():
|
| 685 |
red_runs.append(run)
|
|
|
|
|
|
|
| 686 |
|
| 687 |
-
if not red_runs:
|
| 688 |
return 0
|
| 689 |
|
|
|
|
|
|
|
| 690 |
# Clear all red text first
|
| 691 |
for run in red_runs:
|
| 692 |
run.text = ''
|
| 693 |
|
| 694 |
-
#
|
| 695 |
-
if red_runs:
|
| 696 |
first_run = red_runs[0]
|
| 697 |
-
|
| 698 |
-
|
| 699 |
-
|
| 700 |
-
|
| 701 |
-
first_run.text = str(attendance_list[0]).strip()
|
| 702 |
-
first_run.font.color.rgb = RGBColor(0, 0, 0) # Make it black
|
| 703 |
-
replacements_made += 1
|
| 704 |
|
| 705 |
# Add remaining items with line breaks
|
| 706 |
for item in attendance_list[1:]:
|
| 707 |
item_text = str(item).strip()
|
| 708 |
if item_text:
|
| 709 |
-
|
| 710 |
-
|
| 711 |
-
|
| 712 |
-
|
| 713 |
-
|
| 714 |
-
|
| 715 |
-
|
| 716 |
-
|
| 717 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 718 |
|
| 719 |
return replacements_made
|
| 720 |
|
|
|
|
| 677 |
"""Custom function to replace red text with properly formatted attendance list"""
|
| 678 |
replacements_made = 0
|
| 679 |
|
| 680 |
+
# Find all red text runs and their paragraphs
|
| 681 |
red_runs = []
|
| 682 |
+
target_paragraph = None
|
| 683 |
+
|
| 684 |
for paragraph in cell.paragraphs:
|
| 685 |
for run in paragraph.runs:
|
| 686 |
if is_red(run) and run.text.strip():
|
| 687 |
red_runs.append(run)
|
| 688 |
+
if target_paragraph is None:
|
| 689 |
+
target_paragraph = paragraph
|
| 690 |
|
| 691 |
+
if not red_runs or not target_paragraph:
|
| 692 |
return 0
|
| 693 |
|
| 694 |
+
print(f" 🔧 Found {len(red_runs)} red runs to replace")
|
| 695 |
+
|
| 696 |
# Clear all red text first
|
| 697 |
for run in red_runs:
|
| 698 |
run.text = ''
|
| 699 |
|
| 700 |
+
# Add the first attendance item to the first red run
|
| 701 |
+
if len(attendance_list) > 0 and red_runs:
|
| 702 |
first_run = red_runs[0]
|
| 703 |
+
first_run.text = str(attendance_list[0]).strip()
|
| 704 |
+
first_run.font.color.rgb = RGBColor(0, 0, 0) # Make it black
|
| 705 |
+
replacements_made += 1
|
| 706 |
+
print(f" ✅ Added first item: '{attendance_list[0]}'")
|
|
|
|
|
|
|
|
|
|
| 707 |
|
| 708 |
# Add remaining items with line breaks
|
| 709 |
for item in attendance_list[1:]:
|
| 710 |
item_text = str(item).strip()
|
| 711 |
if item_text:
|
| 712 |
+
try:
|
| 713 |
+
# Method 1: Try to add line break and new run to the same paragraph
|
| 714 |
+
from docx.oxml import OxmlElement
|
| 715 |
+
br = OxmlElement('w:br')
|
| 716 |
+
first_run._element.append(br)
|
| 717 |
+
|
| 718 |
+
# Add the text to the same run after the line break
|
| 719 |
+
first_run.text += item_text
|
| 720 |
+
replacements_made += 1
|
| 721 |
+
print(f" ✅ Added item with line break: '{item_text}'")
|
| 722 |
+
except Exception as e:
|
| 723 |
+
print(f" ⚠️ Line break method failed: {e}")
|
| 724 |
+
# Fallback: try to add as new paragraph
|
| 725 |
+
try:
|
| 726 |
+
new_para = cell.add_paragraph()
|
| 727 |
+
new_run = new_para.add_run(item_text)
|
| 728 |
+
new_run.font.color.rgb = RGBColor(0, 0, 0)
|
| 729 |
+
replacements_made += 1
|
| 730 |
+
print(f" ✅ Added as new paragraph: '{item_text}'")
|
| 731 |
+
except Exception as e2:
|
| 732 |
+
print(f" ❌ Both methods failed: {e2}")
|
| 733 |
|
| 734 |
return replacements_made
|
| 735 |
|