1

After trying for 30 hours+ to implement python_-docx and docxtpl for certain functionalities (and rigulariously failing), I decided to come here for advice.

My current project exists of different pictures (.png), formatted texts (i.e. bold, shadow, font, color and so forth), etc. - now these elements need to be arranged / fit into a neat template. First, I tried pillow by creating a canvas and adding all these elements each. The solution itself is extremely prone to errors and doesn't support all the functionalities as far as text is concerned. Next off, I went by creating a .docx template (arranging pictures, text including font, style, etc.) and implementing the values this way - that worked! ... except of it not supporting more than one picture / media element per Word page!)

For demonstration purposes I tried to sketch the workflow: Workflow

Now it should be obvious why I tried Word - an easy-to-go word editing program in which I was able to format everything to my wishes (though the Python API didn't work, hence it's useless) - for demonstration purposes, here is a snippet of pseudo code:

#PSEUDO CODE

from docxtpl import DocxTemplate

tpl = DocxTemplate('file.docx')
tpl.replace_media('dummy.png', 'pic1.png')
tpl.replace_media('dummy2.png', 'pic2.png')
tpl.save('out.docx')

Depending on the setup, it either replaces None, or both pictures with one of them. According to various StackOverflow questions and threads, more than one picture isn't possible! Therefore the word approach is rather useless.

Anyhow, I'm out of knowledge. Any suggestions on how to achieve such a workflow, i.e. having an easy editable layout in which I just need to parse certain values in and get a .docx, .png, .pdf, whatever..

J. M. Arnold
  • 6,261
  • 3
  • 20
  • 38
  • What exactly do you mean by "not supporting more than one picture per page"? From your image it looks like that you can include two pictures on the same page, no? – user202729 Jan 11 '21 at 03:53
  • Also, you should probably post your existing code. It would be hard to post the docx file, but you can still explain what it contains. – user202729 Jan 11 '21 at 03:54
  • This output could be obtained using some *logic* (regarding the sizes of the regions) while processing the inputs in `pillow` library – Vasu Deo.S Jan 11 '21 at 07:17
  • @user202729 This is the wanted workflow, unfortunately I haven't achieved this. I can show some demo code as far as word is concerned, though I'm sure you can't achieve this with either `python-docx` and `docxtpl` (after researching couple of hours on this matter I read that it's simply not possible to replace multiple pictures on one page). – J. M. Arnold Jan 11 '21 at 10:12
  • @VasuDeo.S I'm not the op, but I guess the problem with pillow is that it isn't easy to do what a word processor can do (bold, italic, etc.) – user202729 Jan 11 '21 at 10:14
  • 1
    Perhaps you can look into reportlab (PDF generation), VBA (perhaps this thing support word better?), ODT (not sure about this one, but I think that since the format is open, more tools would support it), or HTML (and some way to print a HTML page) – user202729 Jan 11 '21 at 10:16
  • @VasuDeo.S Besides PIL being very error-prone, you don't have all the functionalities (not even basic ones). – J. M. Arnold Jan 11 '21 at 10:17
  • @user202729 Thanks for the information! Do you mind answering this question with the help of one example? What is the best library in your opinion for this case (i.e. the HTML website part sounds promising). – J. M. Arnold Jan 11 '21 at 10:18
  • I'm not sure what exactly the question is asking about, but you can just search for "pypi HTML generation from template" or something and pick a popular library. Someone asked [python html generator](https://stackoverflow.com/questions/1548474/python-html-generator) on Stack Overflow, (*the tools recommended there doesn't look very bad actually, although it's a library recommendation question*) – user202729 Jan 11 '21 at 10:23
  • @user202729 According to [this Meta thread](https://meta.stackoverflow.com/questions/254393/what-exactly-is-a-recommendation-question), I'm not asking for a library recommendation. I demonstrated the goal and how/with which libraries I tried unsuccessfully. Now I'm out of knowledge and asking how to solve the issue - whether you actually have a solution with the libraries I failed with or a new one, is up to you! :-) Anyhow, the mentioned SO thread isn't what I was looking for: I need a easily customizable template in which certain elements will be replaced by the "navigator", Python. – J. M. Arnold Jan 11 '21 at 10:26
  • I'm talking about the linked question -- anyway, templess and jinja from that question seems to be HTML template engine -- I'm not familiar with either of them. – user202729 Jan 11 '21 at 10:28

0 Answers0