docx_parser_converter.docx_parsers.styles.styles_merger module
- class docx_parser_converter.docx_parsers.styles.styles_merger.StyleMerger(document_schema: DocumentSchema, styles_schema: StylesSchema, numbering_schema: NumberingSchema)[source]
Bases:
objectA class to merge styles from styles.xml and numbering.xml into the document schema from document.xml. This involves resolving based-on styles, applying numbering properties, and applying default properties.
Rules of Inheritance: 1. Styles Inheritance: - Styles defined in styles.xml can be based on other styles. - Properties from the base styles are inherited unless overridden in the inheriting style. - This is resolved using the resolve_based_on_styles method.
2. Numbering Properties: - Numbering definitions in numbering.xml can specify properties such as indentation. - These properties are applied to paragraphs that have associated numbering. - This is handled by the apply_numbering_properties method.
3. Default Properties: - Default properties can be specified in styles.xml for paragraphs and runs. - These defaults are applied last, filling in any missing properties. - This is handled by the apply_default_properties method.
- apply_default_properties(paragraph: Paragraph)[source]
Applies default properties to a paragraph. This includes applying the default paragraph style and document default properties.
Default Properties Inheritance Rule: - If a paragraph does not have a style_id, apply the default paragraph style properties. - Merge the document default properties (doc_defaults_ppr for paragraphs and doc_defaults_rpr for runs) last to ensure all properties are filled.
- Parameters:
paragraph (Paragraph) – The paragraph to apply default properties to.
Example
The following is an example of document default properties in a styles.xml file:
<w:docDefaults> <w:rPrDefault> ... </w:rPrDefault> <w:pPrDefault> ... </w:pPrDefault> </w:docDefaults>
- apply_numbering_properties(paragraph: Paragraph)[source]
Applies numbering properties to a paragraph.
Numbering Inheritance Rule: - If a paragraph has associated numbering, merge the numbering properties (e.g., indentation) from numbering.xml.
- Parameters:
paragraph (Paragraph) – The paragraph to apply numbering properties to.
Example
The following is an example of a numbering level in a numbering.xml file:
<w:lvl w:ilvl="0"> <w:start w:val="1"/> <w:numFmt w:val="decimal"/> ... </w:lvl>
- apply_style_properties(paragraph: Paragraph)[source]
Applies style properties to a paragraph.
Style Inheritance Rule: - If a paragraph has a style_id, merge the properties from the corresponding style in styles.xml.
- Parameters:
paragraph (Paragraph) – The paragraph to apply style properties to.
Example
The following is an example of a paragraph style in a styles.xml file:
<w:style w:styleId="Heading1" w:type="paragraph"> <w:name w:val="heading 1"/> <w:basedOn w:val="Normal"/> ... </w:style>
- find_style(style_id: str) Style | None[source]
Finds a style by its ID.
- Parameters:
style_id (str) – The ID of the style to find.
- Returns:
The found style, or None if not found.
- Return type:
Optional[Style]
- merge_paragraph_styles(paragraph: Paragraph)[source]
Merges styles into a paragraph.
- Parameters:
paragraph (Paragraph) – The paragraph to merge styles into.
- merge_styles()[source]
Merges styles into the document schema. This involves applying numbering properties, style properties, and default properties.
Inheritance Rule: - Apply numbering properties first if the paragraph has associated numbering. - Then apply style properties defined in styles.xml. - Finally, apply default properties defined in styles.xml.
- resolve_based_on_styles()[source]
Resolves styles that are based on other styles by merging their properties. This ensures that all properties from the base styles are inherited correctly.
Inheritance Rule: - For each style that is based on another style (base style), merge the properties of the base style into the inheriting style. - This process continues recursively for styles based on other styles.
Example
The following is an example of a style based on another style in a styles.xml file:
<w:style w:styleId="Heading1" w:type="paragraph"> <w:name w:val="heading 1"/> <w:basedOn w:val="Normal"/> ... </w:style>