python-openxml / python-docx

Create and modify Word documents with Python
MIT License
4.51k stars 1.11k forks source link

How to iterate over each Sentence using python-docx #962

Closed ilcaa72 closed 3 years ago

ilcaa72 commented 3 years ago

i am currently using VBA on Word and would love to switch to python-docx. So i had a question. Within VBA it has a paragraph and sentence object and i can iterate over every sentence like this... Word VBA know what a paragraph, sentence and word and its easy to iterate and make specific changes.....

How can i accomplish this with python-docx? is the Word VBA object model available to me in python-docx? thanks for any insight.

'//  loop over each sentence and word in a Word document (using VBA IDE in MS Word)
Dim s As Range
For Each s In ActiveDocument.Sentences
              s.Select

             for each wrd in s
                      wrd.Select
     next
next
scanny commented 3 years ago

python-docx has no concept of sentence, word, range, or selection. If you need those you'll have to implement them yourself. python-docx (and indeed WordprocessingML which is what a .docx file is made up of) have only the concepts of paragraph and run. Sentence, word, range, and selection are built on top of these paragraph and run objects, but not in python-docx, at least not yet or for the foreseeable future.