8

I am a bit confused about the concept of 'Run-level content' in python-docx.. I get that if I wanna check whether a paragraph is in bold or not, I need to check the run.bold, but what exactly is it? The official definition is: A run is the object most closely associated with inline content; text, pictures, and other items that are flowed between the block-item boundaries within a paragraph.

So, is it singular character level content in the paragraph? am I missing anything here?

scanny
  • 26,423
  • 5
  • 54
  • 80
Phillysteak
  • 95
  • 1
  • 7

1 Answers1

12

A simple way to understand a run in Word is a sequence of characters that all share the same character formatting.

So if you have a sentence like this and want a bold word to appear, you can't tell the sentence to be bold (that would bold too much) and you don't want to tell each individual character to be bold (that would bold too-little at a time).

So you group the characters into runs and apply character formatting to the run (and that is juuuust right :).

The example sentence would need three runs. One before the bold word, one for the bold word itself, and one for after the bold word. The middle run would be set bold; the other two would have no special formatting.

There are more things to know about runs, like they are subordinate to a paragraph (so the same run can't start in one paragraph and end in another), but this is the main gist of the concept.

scanny
  • 26,423
  • 5
  • 54
  • 80
  • It's quite confusing to me about runs versus sentence - sometimes each sentence is a run, but sometimes a run can contain several sentences. And don't know what I can expect to get by looking at the docx file within MS Word. – user3768495 Dec 15 '19 at 08:53
  • 2
    The only thing guaranteed about a run is that all text within it shares the same character-level formatting. In particular, there is no guarantee that an adjacent run contains _different_ character-level formatting. So runs can effectively break up the text of a paragraph at arbitrary locations, even one run per character. In short, Word doesn't try to keep track of sentences; if you see a run that is a sentence that is pure coincidence. You need to do sentence parsing yourself if you need it, perhaps based on `paragraph.text`. – scanny Dec 15 '19 at 19:43