TLDR: Which takes higher precedence Style Inheritance or Style Hierarchy, when we want to find value in the run properties and it gives back None as the output.?
Example: Printing the font name from all the runs in the document
doc = Document()
for para in doc.paragraphs:
if para.text:
for run in para.runs:
print(run.font.name)
Problem:
Currently, I'm working on a use-case to check whether the given document has a specific font/language/font size, etc,...
For example: In order to get the language (spelling and grammar) for the entire document. Currently, I follow the below steps to get values using Style Hierarchy:
- Load the document
- Iterate through paragraphs
- Iterate through the runs of a paragraph
- Check it's(current run) rPr for
lang - If there is no value from run, then check the
rProf the current para forlang - If there is no value from para, then get value from
docDefaults rPr - If I get a value between Step 4-6, I'll consider that value as run's value
This approach helped me to get the expected result for both lang and font (at least for the document I have).
But for some cases, the docDefaults itself won't have a value and this approach doesn't help, then I heard about Style Inheritance.
So I just implemented a recursive function to get the value from the base_style, if the current style doesn't have a value.
I made a minor change in my implementation as, Whenever there is no value from the rPr, I just check it's base_style for value, before moving one step up in the hierarchy.
I'm not sure whether the approach I'm following is correct. If there is an easy way to achieve this. Please help. References