Search code examples
python-3.xpandasdocxpython-docxdoc

How to Edit the Imported Word Document using python


I do have a word document and I want to edit it. Here is the part of the document. [Part of Word Document]: https://i.sstatic.net/g5JGO.jpg

I could upload it into jupyter notebook using python-docx.

I can access to each line by

import docx
doc = docx.Document('StudentReport.docx')
len(doc.paragraphs)
output- 31

print(doc.paragraphs[7].text)
output- 98% of Student have some access

So I just want to change that 98% to 85%.


Solution

  • You can just plainly set it as:

    doc.paragraphs[7].text = '85% of Student have some access'
    

    In case you want to be teeny tiny bit more fancy about it:

    doc.paragraphs[7].text = doc.paragraphs[7].text.replace('98%','85%')