docx_parser_converter.docx_parsers.styles.styles_parser module

class docx_parser_converter.docx_parsers.styles.styles_parser.StylesParser(source: bytes | str | None = None)[source]

Bases: object

A parser for extracting styles from a DOCX file.

extract_doc_defaults_ppr(root) ParagraphPropertiesParser[source]

Extracts the default paragraph properties from the styles XML.

Parameters:

root (ET.Element) – The root element of the styles XML.

Returns:

The parsed default paragraph properties.

Return type:

ParagraphPropertiesParser

Example

The following is an example of default paragraph properties in a styles.xml file:

<w:docDefaults>
    <w:pPrDefault>
        <w:pPr>
            <w:spacing w:before="120" w:after="120"/>
        </w:pPr>
    </w:pPrDefault>
</w:docDefaults>
extract_doc_defaults_rpr(root) RunPropertiesParser[source]

Extracts the default run properties from the styles XML.

Parameters:

root (ET.Element) – The root element of the styles XML.

Returns:

The parsed default run properties.

Return type:

RunPropertiesParser

Example

The following is an example of default run properties in a styles.xml file:

<w:docDefaults>
    <w:rPrDefault>
        <w:rPr>
            <w:rFonts w:ascii="Calibri" w:hAnsi="Calibri"/>
            <w:sz w:val="22"/>
        </w:rPr>
    </w:rPrDefault>
</w:docDefaults>
extract_style(style_element: Element) Style[source]

Extracts a single style from the styles XML element.

Parameters:

style_element (ET.Element) – The style XML element.

Returns:

The extracted style.

Return type:

Style

Example

The following is an example of a style element 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:pPr>
        <w:spacing w:before="240" w:after="240" w:line="360"/>
    </w:pPr>
    <w:rPr>
        <w:rFonts w:ascii="Calibri" w:hAnsi="Calibri"/>
    </w:rPr>
</w:style>
extract_style_type_defaults(root) StyleDefaults[source]

Extracts the default styles from the styles XML.

Parameters:

root (ET.Element) – The root element of the styles XML.

Returns:

The extracted default styles.

Return type:

StyleDefaults

Example

The following is an example of default styles in a styles.xml file:

<w:styles>
    <w:style w:styleId="DefaultParagraphFont" w:type="character" w:default="1">
        <w:name w:val="Default Paragraph Font"/>
    </w:style>
    <w:style w:styleId="Normal" w:type="paragraph" w:default="1">
        <w:name w:val="Normal"/>
    </w:style>
</w:styles>
get_styles_schema() StylesSchema[source]

Returns the parsed styles schema.

Returns:

The parsed styles schema.

Return type:

StylesSchema

parse() StylesSchema[source]

Parses the styles XML and returns the StylesSchema.

Returns:

The parsed styles schema.

Return type:

StylesSchema

Example

The following is an example of a styles element in a styles.xml file:

<w:styles>
    <w:style w:styleId="Heading1" w:type="paragraph">
        <w:name w:val="heading 1"/>
        <w:basedOn w:val="Normal"/>
        <w:pPr>
            <w:spacing w:before="240" w:after="240" w:line="360"/>
        </w:pPr>
        <w:rPr>
            <w:rFonts w:ascii="Calibri" w:hAnsi="Calibri"/>
        </w:rPr>
    </w:style>
    <w:docDefaults>
        <w:rPrDefault>
            <w:rPr>
                <w:rFonts w:ascii="Calibri" w:hAnsi="Calibri"/>
                <w:sz w:val="22"/>
            </w:rPr>
        </w:rPrDefault>
        <w:pPrDefault>
            <w:pPr>
                <w:spacing w:before="120" w:after="120"/>
            </w:pPr>
        </w:pPrDefault>
    </w:docDefaults>
</w:styles>