简体   繁体   中英

Python - Insert data into a word document using a list

I have a word document with data and I have 2 lists with phrases. I would like to loop through the word document and wherever a specific keyword is found (so keyword_one and keyword_two in this instance), add a new line and insert the first item from the relevant list.

My code below is only adding one item from one list to keyword_one and I think it could be to do with the for loop structure - please could someone point me in the right direction?

Code:

document = Document('mydocx.docx')
my_list=['Some key points here', 'We expected values to be higher. That is fine', 'final attributes', 'local dataset'] 
my_second_list=['My random data', 'Flowers. That is fine', 'happy birthday', 'puppies']

for para in document.paragraphs:
    print(para.text)
    for i in my_list:
        for j in my_second_list:
            if 'Keyword_one' in para.text: 
                para.add_run('         ' +i)
            if 'Keyword_two' in para.text:
                para.add_run('      ' + j)
        else:
            break
            
document.save("mydocx.docx")

Desired example output:

My ms word document.

Keyword_one
Some key points here. I have some additional data
Other data here

Keyword_two 
Flowers

Heading 1

Keyword_one
We expected values to be higherThat is fine'

Keyword_one 
final attributes

Keyword_two 
My random data. Other random data here

Heading 1

Keyword_two 
Flowers

I think this should give you the result you are looking for:

from docx import Document

document = Document('mydocx.docx')
my_list = ['Some key points here', 'We expected values to be higher. That is fine', 'final attributes', 'local dataset'] 
my_second_list = ['My random data', 'Flowers. That is fine', 'happy birthday', 'puppies']

i = 0
j = 0
for para in document.paragraphs:
   if 'Keyword_one' in para.text and i < len(my_list):
      para.add_break()
      para.add_run(my_list[i])
      i += 1
   elif 'Keyword_two' in para.text and j < len(my_second_list):
      para.add_break()
      para.add_run(my_second_list[j])
      j += 1

document.save("mydocx.docx")

In the case where a paragraph contains both keywords and you want it to add from both lists, change the elif to if .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM