![]() Tornado.template — Flexible output generation¶ A simple template system that compiles templates to Python code. Basic usage looks like. News about the dynamic, interpreted, interactive, object-oriented, extensible programming language Python If you are about to ask a question, please consider. Homework-style questions will be removed, and you'll be encouraged to post there instead. ![]() Python Template SystemPlease don't use URL shorteners. Reddit filters them out, so your post or comment will be lost. I need to generate a pretty customer-facing report, based on data in a database. I also need to be able to manually edit the document before sending to the customer. Having looked at a few options (PDF, HTML) it appears that a Word (or at least docx) document would work best: it has WYSIWYG editing, spell checking, track changes, etc. Python Template SystemIt seems to be difficult to generate complex docx files from Python. For example, python-docx is ok for simple documents, but to generate pretty tables and such would require masses of code. I've also looked at generating the XML directly using XSLT, but this is even worse. I'm working in Python, but open to using any language to get this solved. 6'1 in cm. Python Template StringsWhat I really want is effectively a template system inside Word. I want to define tags that are dynamically replaced by data. I'm aware that Word has mail merge functionality, which can do this to some extent. However, it appears to be somewhat limited. In particular, I've seen no way to do 'for each'. I wonder if for-each could be done using VBA - but I don't know how just now. I figure other people must have this requirement - any ideas? The DocX XML file is tricky. You can have a valid XML file but the output would not look exactly as you wanted. The main issue I see are w:p runs. Some items could be childs of them and some don't. You can surely use a DTD to validate but it's a PITA, at least for me. It would definitely work for a complete table for example, if you replace the exact same type of object. I mean, open up word, create an empty table, unzip the docx, open the document.xml and find the table element and then replace the whole table element. That would work just fine:) • • • • •. I've built a fairly substantial system similar to what you describe and could not find an appropriate python library. I ended up using docxtemplater, which takes a.json file as input and populates a word file. My python code grabs the data and dumps it into a.json file which is then passed to docxtemplater. From Python I basically pass a dictionary of key:value pairs and get back a docx file. I cannot say enough good things about docxtemplater. The tag system is simple but incredibly powerful. Looping in particular is almost magical and works seamlessly and as expected even when dealing with nested lists and tables. I considered porting docxtemplater to python but couldn't justify the time commitment. You have several options with images, I link to external images and overwrite them when they change since this allows you to format, resize and position the image. Microsoft Word really dislikes relative paths and will unfailingly convert everything to absolute paths and then break if the file is moved though.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |