Locations to Process
- Main body paragraphs:
doc.paragraphs - Table cells: Iterate
doc.tables→ rows → cells → paragraphs - Headers:
doc.sections[*].header.paragraphs - Footers:
doc.sections[*].footer.paragraphs
Algorithm
def get_all_paragraphs(doc):
paragraphs = []
# Main body
paragraphs.extend(doc.paragraphs)
# Tables
for table in doc.tables:
for row in table.rows:
for cell in row.cells:
paragraphs.extend(cell.paragraphs)
# Headers and footers
for section in doc.sections:
paragraphs.extend(section.header.paragraphs)
paragraphs.extend(section.footer.paragraphs)
return paragraphs
Processing Strategy
- Use this function when you need to process all text locations (replacements, conditional handling, validation)
- Maintain iteration order to ensure markers like
{{IF_*}}and{{END_IF_*}}are encountered in the correct sequence - Collect elements to delete first, then delete after iteration completes