I have a grand total of 3 weeks of very part time experience with Python
This is close as I have gotten with somebody else’s code
import win32com.client
def iter_headings(paragraphs):
for paragraph in paragraphs:
#The code crashes on the next line!!!!!!!!!!!!!!!!!!
if paragraph.Style.Name.startswith('Heading'):
yield paragraph
# Create a Word application instance
word = win32com.client.Dispatch('Word.Application')
# Open the Word document
document = word.Documents.Open('d:\header.docx')
# Access paragraphs using the Document object
for heading in iter_headings(document.Paragraphs):
print(heading.Text)
# Close the Word document (optional)
document.Close()
word.Quit()`
Any ideas to help would be welcome! I am also using win32com.client to read in the contents of multiple tables in the document and that is working well in another test file, so I would prefer to stick with with win32com if possible.
See the above code.
New contributor
Tom is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.