Home > OS >  TypeError: unsupported operand type(s) for : 'Paragraph' and 'str'. Replace wor
TypeError: unsupported operand type(s) for : 'Paragraph' and 'str'. Replace wor

Time:06-07

I want to Find and replace text which is only after a paragraph.

Text in my word document:

image of text in my word document

As you can see, I want to replace the Word Ocean which is at the end. So, I want to replace the word 'Ocean' which is only after a paragraph

My Code so far:-

from docx import Document

document = Document('rea.docx')

for paragraph in document.paragraphs:
    if paragraph 'Ocean' in paragraph.text:
        paragraph.text.replace("Sea")

I want to replace the Words which are after a paragraph. I also want to replace words which are after two paragraphs like this :

image of word after two paragraphs

I also want to replace words which are after three paragraphs like this:

image of word after three paragraphs

How can i Write a code which can replace words which are after a paragraph and after two paragraphs and which are after three paragraphs?

enter image description here

in the above Picture, I want to replace :-

Ocean:sea Plants:Seeds Flowers:Plants

So, I want to replace Only specific words if a paragraph precedes it

There are so many words i should replace if more than a paragraph precedes it.

My:you

Bone:Muscle

Speaker:Magnet

Bin:Dustbin

Pen:Pencil

Note:- I want to replace the above words only if more than a paragraph precedes it.

So, the output will look like: enter image description here

How to create a paragraph before the specific word ?

example:- I have the word different.

Before:-

enter image description here

After:-

enter image description here

CodePudding user response:

Your task appears to be to detect an empty paragraph followed by a non-empty paragraph.

As I found out some time ago, checking for empty paragraphs is non-trivial in a word document: How to detect an empty paragraph in python-docx. However, if you're only dealing with text, it's made simple by the text attribute. You can therefore set a flag whenever an empty is found, and replace text whenever the flag is set.

Let's say you have a dictionary of replacements:

replacements = {
    'Ocean': 'sea',
    'Plants': 'Seeds',
    'Flowers': 'Plants',
}

Notice that order is important: if you replace Flowers with Plants before Plants with Seeds, you will run into a problem. I'm assuming you're using Python >= 3.6, where regular dictionaries are ordered.

You have a couple of options going forward. One is to make a function that replaces a single word in the document, and call it for each word:

def replace(doc, target, replacement):
    prev_is_empty = False
    for par in doc.paragraphs:
        if par.text == '':
            prev_is_empty = True
        elif par_is_empty:
            par.text = par.text.replace(target, replacement)
            prev_is_empty = False

for item in replacements.items():
    replace(document, *item)

Another option is to replace the whole dictionary at once for each relevant paragraph:

prev_is_empty = False
for par in document.paragraphs:
    if par.text == '':
        prev_is_empty = True
    elif par_is_empty:
        s = par.text
        for item in replacements.items():
            s = s.replace(*item)
        par.text = s
        prev_is_empty = False
  • Related