python-openxml / python-docx

Create and modify Word documents with Python
MIT License
4.56k stars 1.12k forks source link

Remove lnNumType tag from sectPr #465

Open pythonGuySushant opened 6 years ago

pythonGuySushant commented 6 years ago

I am trying to remove lnNumType tag from sectPr to remove line numbering, Here is the snippet,

actual=bodyBod.xml 
import lxml.etree
doc = lxml.etree.fromstring(actual)
for child in doc.iter():
    if "lnNumType" in child.tag:
        child.remove
        print "this is working",child.getparent().remove(child)
from lxml.etree import tostring
print 'final =======   ',tostring(doc)

Have successfully removed the tag <w:lnNumType w:countBy="1" w:restart="continuous"/> but I am not able to reassign to "bodyBod.xml" , this is how I get this variable

 bodyObject=document._Document__body
bodyBod=bodyObject._body
actual=bodyBod.xml

This is the error when assigning

 bodyBod.xml=docx.oxml.xmlchemy.XmlString(tostring(doc))
AttributeError: can't set attribute

Help me. Any other approach to remove line number.

ayrtonnotrya commented 1 year ago

doc = docx.Document(filename) if doc.sections[0]._sectPr.xpath("w:lnNumType"): doc.sections[0]._sectPr.remove(doc.sections[0]._sectPr.xpath("w:lnNumType")[0]) doc.save(filename)