How to use python-docx to replace text in a Word document and save

How to use python-docx to replace text in a Word document and save

You can use the python-docx library to replace text in a Word document and then save the modified document. Here's how you can do it:

from docx import Document def replace_text(doc, old_text, new_text): """ Replace all occurrences of old_text with new_text in the document. """ for p in doc.paragraphs: if old_text in p.text: inline = p.runs for i in range(len(inline)): if old_text in inline[i].text: text = inline[i].text.replace(old_text, new_text) inline[i].text = text def main(): # Open the Word document doc = Document('your_document.docx') # Replace text replace_text(doc, 'old_text', 'new_text') # Save the modified document doc.save('modified_document.docx') if __name__ == "__main__": main() 

In this example:

  • Replace 'your_document.docx' with the path to your Word document.
  • The replace_text function replaces all occurrences of old_text with new_text in the document.
  • We loop through all paragraphs in the document and check if old_text is present. If found, we replace it with new_text.
  • Finally, we save the modified document using the save method.

Make sure to replace 'old_text' with the text you want to replace and 'new_text' with the text you want to replace it with. Additionally, adjust the file paths as needed to point to your input and output files.

Examples

  1. "Python-docx replace text in Word document example"

    Description: This query seeks guidance on using python-docx to find and replace specific text within a Word document programmatically.

    from docx import Document def replace_text(doc, old_text, new_text): for paragraph in doc.paragraphs: if old_text in paragraph.text: paragraph.text = paragraph.text.replace(old_text, new_text) # Load the Word document doc = Document("your_document.docx") # Replace "old_text" with "new_text" replace_text(doc, "old_text", "new_text") # Save the modified document doc.save("modified_document.docx") 

    Implementation: This code defines a function to replace occurrences of a specified text within paragraphs of a Word document using python-docx.

  2. "Python-docx find and replace text in Word document"

    Description: This query aims to find a solution for searching and replacing text in a Word document using the python-docx library.

    from docx import Document def find_replace(doc, target, replacement): for p in doc.paragraphs: if target in p.text: p.text = p.text.replace(target, replacement) # Load the Word document doc = Document("your_document.docx") # Find and replace "target_text" with "replacement_text" find_replace(doc, "target_text", "replacement_text") # Save the modified document doc.save("modified_document.docx") 

    Implementation: This code snippet defines a function to find and replace text in a Word document using python-docx, iterating through paragraphs to perform replacements.

  3. "Python-docx substitute text in Word document"

    Description: Users searching for this query are looking for ways to substitute specific text with another in a Word document using python-docx.

    from docx import Document def substitute_text(doc, old_text, new_text): for paragraph in doc.paragraphs: if old_text in paragraph.text: paragraph.text = paragraph.text.replace(old_text, new_text) # Load the Word document doc = Document("your_document.docx") # Substitute "old_text" with "new_text" substitute_text(doc, "old_text", "new_text") # Save the modified document doc.save("modified_document.docx") 

    Implementation: This code defines a function to substitute occurrences of a specified text within paragraphs of a Word document using python-docx.

  4. "Python-docx replace text in Word file"

    Description: This query seeks information on how to programmatically replace text within a Word file using the python-docx library.

    from docx import Document def replace_text(doc, old_text, new_text): for paragraph in doc.paragraphs: if old_text in paragraph.text: paragraph.text = paragraph.text.replace(old_text, new_text) # Load the Word document doc = Document("your_document.docx") # Replace "old_text" with "new_text" replace_text(doc, "old_text", "new_text") # Save the modified document doc.save("modified_document.docx") 

    Implementation: This code snippet defines a function to replace occurrences of a specified text within paragraphs of a Word document using python-docx.

  5. "Python-docx find and replace text example"

    Description: Users seeking this query are looking for examples or tutorials on how to perform find and replace operations on a Word document using python-docx.

    from docx import Document def find_replace(doc, target, replacement): for p in doc.paragraphs: if target in p.text: p.text = p.text.replace(target, replacement) # Load the Word document doc = Document("your_document.docx") # Find and replace "target_text" with "replacement_text" find_replace(doc, "target_text", "replacement_text") # Save the modified document doc.save("modified_document.docx") 

    Implementation: This code defines a function to find and replace text in a Word document using python-docx, iterating through paragraphs to perform replacements.

  6. "Replace text in Word document using python-docx"

    Description: This query is about replacing specific text within a Word document programmatically using the python-docx library.

    from docx import Document def replace_text(doc, old_text, new_text): for paragraph in doc.paragraphs: if old_text in paragraph.text: paragraph.text = paragraph.text.replace(old_text, new_text) # Load the Word document doc = Document("your_document.docx") # Replace "old_text" with "new_text" replace_text(doc, "old_text", "new_text") # Save the modified document doc.save("modified_document.docx") 

    Implementation: This code snippet defines a function to replace occurrences of a specified text within paragraphs of a Word document using python-docx.

  7. "Python-docx replace text in Word document with another"

    Description: This query is aimed at replacing specific text with another text within a Word document using the python-docx library.

    from docx import Document def replace_text(doc, old_text, new_text): for paragraph in doc.paragraphs: if old_text in paragraph.text: paragraph.text = paragraph.text.replace(old_text, new_text) # Load the Word document doc = Document("your_document.docx") # Replace "old_text" with "new_text" replace_text(doc, "old_text", "new_text") # Save the modified document doc.save("modified_document.docx") 

    Implementation: This code defines a function to replace occurrences of a specified text within paragraphs of a Word document using python-docx.

  8. "Python-docx substitute text in Word document example"

    Description: This query seeks an example or tutorial on how to substitute specific text with another text in a Word document using python-docx.

    from docx import Document def substitute_text(doc, old_text, new_text): for paragraph in doc.paragraphs: if old_text in paragraph.text: paragraph.text = paragraph.text.replace(old_text, new_text) # Load the Word document doc = Document("your_document.docx") # Substitute "old_text" with "new_text" substitute_text(doc, "old_text", "new_text") # Save the modified document doc.save("modified_document.docx") 

    Implementation: This code snippet defines a function to substitute occurrences of a specified text within paragraphs of a Word document using python-docx.


More Tags

installshield multibranch-pipeline nsattributedstring master-slave navicat decimalformat dialogfragment height connector angularjs-material

More Programming Questions

More Retirement Calculators

More Financial Calculators

More Animal pregnancy Calculators

More Geometry Calculators