Python docx: insert_after()? -
right now, have "template file" several paragraphs exist (like education, career, aims, etc.) , every paragraph composed like
1. education 2. career 3. aims
now, i'd fill these paragraphs text database make like
1. education education text database 2. career nasty career stuff database 3. aims aims defined here database
searched through api documentation found close function called
insert_paragraph_before(text)
- counterpart exist? desired output should be:
from docx import document document = document('template.docx') p in document.paragraphs: if p.text == 'some keyword here': p.insert_paragraph_after('some text insert here') ^ | function not (yet?) exist document.save('result.docx')
the short answer "not yet".
what might able though:
def insert_paragraph_after(paragraphs, idx, text=none): next_paragraph_idx = idx + 1 if idx == len(paragraphs): return document.add_paragraph(text) next_paragraph = paragraphs[next_paragraph_idx] return next_paragraph.insert_paragraph_before(text) paragraphs = list(document.paragraphs) idx, paragraph in enumerate(paragraphs): if paragraph.text == 'some keyword': insert_paragraph_after(paragraphs, idx, "some lorem ipsum here")
Comments
Post a Comment