I want to change a paragraph of a document by changing the XML. For example I need to change the indent after a paragraph:
import docx
from docx.oxml import OxmlElement
from docx.oxml.ns import qn
doc = docx.Document('example.docx')
paragraphs = doc.paragraphs
for paragraph in doc.paragraphs:
p = paragraph._p
pPr = OxmlElement('w:pPr')
spacingp = OxmlElement('w:spacing')
pPr.append(spacingp)
p.append(pPr)
afterp = OxmlElement('w:after')
afterp.set(qn('w:after'), '0')
lineRulep = OxmlElement('w:lineRule')
lineRulep.set(qn('w:lineRule'), 'auto')
linep = OxmlElement('w:line')
linep.set(qn('w:line'), '240')
spacingp.append(afterp)
spacingp.append(lineRulep)
spacingp.append(linep)
pPr.append(spacingp)
p.append(pPr)
doc.save('restyled.docx')
But this code doesn't work. Please tell me why it doesn't work?