Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23276

Using Python win32com.client to import Word document HEADINGS (not Headers)

$
0
0

I have a grand total of 3 weeks of very part time experience with PythonThis is close as I have gotten with somebody else's code

import win32com.clientdef 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 instanceword = win32com.client.Dispatch('Word.Application')# Open the Word documentdocument = word.Documents.Open('d:\\header.docx')# Access paragraphs using the Document objectfor 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.


Viewing all articles
Browse latest Browse all 23276

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>