I'm trying to use a word document to change it's contents. When I try the following code, it does not work because 'Document' object is not iterable
.
from docx import Document
doc = Document('SomeDocument.docx')
doc_list = list(doc)
some_list = []
for item in doc_list:
if item == 'something':
some_list.append(item)
some_list.save('DocumentOutput.docx')
In order to access the text in a Word document, you need to use the text
function from docx-python
. If you want to manipulate the text of the document you may use a list by storing the text in it, then do what ever you want with it.
doc = Document('SomeDocument.docx')
paragraphs = []
for paragraph in doc.paragraphs:
p = paragraph.text
paragraphs.append(p)
output = Document()
for item in questions_answers:
line = test.add_paragraph(item)
output.save('OutputDocument.docx')
Please Note: This code only copies the text of the document without all the bold, italic, underlined nor colored parts as they are (only their text). Neither will it copy the different fonts, table styles etc. If you do want to copy the styles of each paragraph, please refer to How do I copy the contents of a word document? .