Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Render simple HTML into docx using docxtpl

I'm using python module docxtpl to print data inside a docx template written with jinja2. Works great so far but i need to render in the docx some simple HTML like this:

<h2>Some title</h2>
<h4>Lorem&nbsp;ipsum <strong>dolor sit amet</strong>, consectetur adipisicing elit. Possimus, aliquam,
    minima fugiat placeat provident optio nam reiciendis eius beatae quibusdam!</h4>
<p style="font-size: 18px;">The text is derived from Cicero's De Finibus Bonorum et Malorum (On the Ends of Goods
    and Evils, or alternatively [About] The Purposes of Good and Evil). The original passage began: Neque porro
    quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit (Translation: "Neither is
    there <del>anyone</del> who loves grief itself since it is grief and thus wants to obtain it").</p>
<table class="table">
    <tbody>
        <tr>
            <td>Test</td>
            <td>Test1</td>
            <td>Test2</td>
            <td>Test3</td>
        </tr>
        <tr>
            <td>Lorem</td>
            <td>Lorem1</td>
            <td>Lorem2</td>
            <td>Lorem3</td>
        </tr>
        <tr>
            <td>Ipsum</td>
            <td>Ipsum1</td>
            <td>Ipsum2</td>
            <td>Ipsum3</td>
        </tr>
    </tbody>
</table>

Unfortunately I can't use RichText() from docxtpl to render tables and other html stuff.

I tried to come up with some solutions but I want to understand if there are better ways besides, for example, merge one docx generated from html usign htmldocx with the one generated using docxtpl or get the content with python-docx module from one docx and insert it into the other.

In the worst case scenario, I am also willing to switch to JavaScript/BASH.

like image 601
Mario Avatar asked Sep 05 '25 03:09

Mario


1 Answers

from htmldocx import HtmlToDocx

new_parser = HtmlToDocx()
new_parser.parse_html_file("html_filename", "docx_filename")
#Files extensions not needed, but tolerated

This should work like a charm to convert html in docx.

Not sure I understand your merge problem. The best would certainly be to have your docx template in html instead. Then once you dumped in a single html file everything you need, you convert to docx.

In case you want to merge/insert docx together or so, you can have a look here: How do I append new data to existing doc/docx file using Python

like image 139
Synthase Avatar answered Sep 07 '25 20:09

Synthase