### Install docxbuilder Source: https://docxbuilder.readthedocs.io/en/latest/docxbuilder.html Install the base docxbuilder package using pip. ```bash pip install docxbuilder ``` -------------------------------- ### Configure docx Documents and Styles Source: https://docxbuilder.readthedocs.io/en/latest/docxbuilder.html Example configuration in conf.py for docx_documents, docx_style, and docx_pagebreak_before_section. ```python docx_documents = [ ('index', 'docxbuilder.docx', { 'title': 'Docxbuilder documentation', 'created': 'author' 'subject': 'Sphinx builder extension', 'keywords': ['sphinx'] }, True), ] docx_style = 'path/to/custom_style.docx' docx_pagebreak_before_section = 1 ``` -------------------------------- ### Install docxbuilder with Math Support Source: https://docxbuilder.readthedocs.io/en/latest/docxbuilder.html Install docxbuilder with extra packages for math directive support. ```bash pip install docxbuilder[math] ``` -------------------------------- ### Define Setup Function for Third-Party Extension Node Handling Source: https://docxbuilder.readthedocs.io/en/latest/third_party_extensions.html This function is defined in `conf.py` to register custom visit methods for nodes generated by third-party extensions. It specifically shows how to handle `plantuml` nodes from `sphinxcontrib.plantuml` by defining a `docx_visit_plantuml` function and attaching it to the Docxbuilder translator. ```python # Define setup function in conf.py def setup(app): # Define visit method for plantuml node generated by sphinxcontrib.plantuml # https://pypi.org/project/sphinxcontrib-plantuml/ def docx_visit_plantuml(self, node): def get_filepath(self, node): from sphinxcontrib import plantuml _, filepath = plantuml.render_plantuml(self, node, 'png') return filepath alt = node.get('alt', (node['uml'], None)) # Docxbuilder provides useful methods. See Docxbuilder API reference. self.visit_image_node(node, alt, get_filepath) # Add the visit method to Docxbuilder import docxbuilder translator = docxbuilder.DocxBuilder.default_translator_class setattr(translator, 'visit_plantuml', docx_visit_plantuml) ``` -------------------------------- ### Build docx Document Source: https://docxbuilder.readthedocs.io/en/latest/docxbuilder.html Use the 'make docx' command or 'sphinx-build' to generate the docx document. ```bash make docx # or sphinx-build -b docx path/to/source path/to/output ``` -------------------------------- ### Configure Custom Styles Source: https://docxbuilder.readthedocs.io/en/latest/customization.html Define custom styles for elements like 'strike' and 'custom-table' by mapping class names to style names in the style file. Ensure the specified styles are defined within the custom style file. ```python docx_style = 'path/to/custom-style.docx' docx_style_names = { 'strike': 'Strike', 'custom-table': 'Custom Table', } # And define Strike and Custom Table styles in the style file specified docx_style ``` -------------------------------- ### DocxBuilder Class Source: https://docxbuilder.readthedocs.io/en/latest/api.html The DocxBuilder class handles the overall document building process. It provides methods for initialization, managing outdated documents, retrieving target URIs, preparing for writing, writing documents, and finishing the build. ```APIDOC ## Class: DocxBuilder ### Description Handles the overall document building process. ### Methods - **`default_translator_class`** - alias of `docxbuilder.writer.DocxTranslator` - **`init()`** - Load necessary templates and perform initialization. The default implementation does nothing. - **`get_outdated_docs()`** - Return an iterable of output files that are outdated, or a string describing what an update build will build. - **`get_target_uri(docname, typ=None)`** - Return the target URI for a document name. - **`prepare_writing(docnames)`** - A place where you can add logic before `write_doc()` is run. - **`write_doc(docname, doctree)`** - Where you actually write something to the filesystem. - **`finish()`** - Finish the building process. The default implementation does nothing. ``` -------------------------------- ### Add docxbuilder to Sphinx Extensions Source: https://docxbuilder.readthedocs.io/en/latest/docxbuilder.html Configure your Sphinx project's conf.py to include 'docxbuilder' in the extensions list. ```python extensions = [ ... 'docxbuilder', ... ] ``` -------------------------------- ### Arrange Table in Landscape Page Source: https://docxbuilder.readthedocs.io/en/latest/customization.html Use the 'docx-landscape' class to render a table in landscape orientation. This is particularly useful for tables with many columns or wide figures. ```rst .. csv-table:: :class: docx-landscape A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26 ``` -------------------------------- ### Specify Section Settings with Custom Classes Source: https://docxbuilder.readthedocs.io/en/latest/customization.html Apply specific section settings (e.g., portrait orientation) from a style file to different parts of the document using custom rst-class directives. The directive 'docx-section-portrait-N' applies the Nth section setting. ```rst Section A ========= contents .. Use 2nd section from the next section .. rst-class:: docx-section-portrait-1 Section B ========= contents .. Use 1st section from the next section .. rst-class:: docx-section-portrait-0 Section C ========= contents ``` -------------------------------- ### Apply Custom Styles in reStructuredText Source: https://docxbuilder.readthedocs.io/en/latest/customization.html Use custom roles for character styles and the 'class' directive for table styles to apply user-defined formatting. Currently, only table and character elements support user-defined styles. ```rst .. Use role to specify character class name. .. role:: strike This :strike:`text` is striked. .. list-table:: Custom style table :class: custom-table * - Row1: Col1 - Row1: Col2 * - Row2: Col1 - Row2: Col2 ``` -------------------------------- ### DocxTranslator Class Source: https://docxbuilder.readthedocs.io/en/latest/api.html The DocxTranslator class is a visitor designed to generate a docx document. It includes methods for visiting various node types like admonitions, images, and handling unknown nodes. ```APIDOC ## Class: DocxTranslator ### Description Visitor to generate a docx document. ### Variables - **`builder`** (Sphinx builder) – Sphinx builder. ### Methods - **`visit_admonition_node(node, add_title=False)`** - Insert a table of admonition represented by the node. This function shall be called from visit method for the node. - Parameters: - **node** (A node which represents an admonition.) - **add_title** (If add_title is true, the text corresponding to the node tagname is used as the admonition title. If false, the first child of the node is used.) - **`depart_admonition_node(node, style=None, align='center', margin='10%')`** - Insert a table of admonition represented by the node. This function shall be called from depart method for the node. - Parameters: - **node** (A node which represents an admonition.) - **style** (A style name applied to the admonition table. If style is None, the node’s admonition class or tagname is used.) - **align** (Alignment of the admonition table.) - **margin** (A total margin between the table and page.) - **`visit_image_node(node, alt, get_filepath)`** - Insert an image represented by the node. This function shall be called from visit method for the node. - Parameters: - **node** (A node which represents an image.) - **alt** (An alternative text used when get_filepath is unable to return a valid image path. alt may be a tuple of the text and a string which specified an language in order to highlight the text.) - **get_filepath** (A function which extract a path of the image from the node. This function must take the translator as first parameter, and the node as second parameter.) - **`unknown_visit(node)`** - Called when entering unknown Node types. Raise an exception unless overridden. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.