-1

I have a table in docx in which i want the rows to be added to table only if the substring exists in the string. have included the image of my template for table here : template for docx table

enter image description here i have a dataset x with are set of names and and numbers eg: {1:{ name:aryan,number:1},2:{anand,2},3:{aishwarya,3}}. i wish to create a table such that i populate the table rows such that i only get the numbers of names that contain substring "arya".the image contains what i was able to make, but having issue with a complex condition in the %tr tag,please help with this please help implementing this correctly.

required output: |C1 | |--------------| | 1 | | 3 |

i am able to filter the data, but additional rows and spaces are added to the table bacuse of the for and endfor loop please help in removing this.

from docxtpl import DocxTemplate

tpl = DocxTemplate('templates/dynamic_table_tpl.docx')
context  = {1:{ name:aryan,number:1},2:{name:anand,number:2},3:{name:aishwarya,number:3}}

tpl.render(context)
tpl.save('output/dynamic_table.docx')

template(as in picture):

|C1   |c2    |c3              |c4.    |c5.         |
|--------------------------------------------------|
| {%tr for i in x %} {%tr if "arya" in i.name %}   |
|--------------------------------------------------|
|           {{ i.number}}                          |
---------------------------------------------------|
|{%tr endif %}{%tr endfor %}                       |
----------------------------------------------------
arya nair
  • 1
  • 4
  • 1
    please don't include images. Include your code *directly* – 2e0byo Oct 04 '21 at 10:47
  • 1
    If you do that, you might notice that you have included a perfectly useless image twice, which contains *no code at all* (and certainly not a template). Whilst you're at it, explain what 'only if the substring exists in the string' means (which string?). What you're asking is eminently doable, but to answer we need to see the question, not guess at it in the void ;) – 2e0byo Oct 04 '21 at 10:48
  • I really want to help, believe me, but your question simply does not make sense. *what* do you want to do with jinja2? *what* is a 'set of names'? You need to provide some *code* (even if you don't know how to implement it: provide the desired input and output). Do this by editing the question---that way everyone can see it quickly without scrolling the comments. – 2e0byo Oct 04 '21 at 10:58
  • right, that's a bit clearer. Please post the *code* you used to make that table (I have no idea how to make docx tables with jinja, and I am not going to go and find out). And wrap your code in ``` so it renders nicely :) – 2e0byo Oct 04 '21 at 11:05
  • the pic i posted is the content of tpl – arya nair Oct 04 '21 at 11:11
  • Yes, I can see that. Which is of no use to people who don't know how to make docx tables, but *do* know how to make jinja templates. Could you add *your jinja template* to the question? And clarify where your data comes from: currently the simplest solution is just to delete the entries you don't want, but presumably there's some reason not to do that. – 2e0byo Oct 04 '21 at 11:13
  • 'difficulty with a complex condition in the tr tag' => we need to *see* the tr tag, and *see* how you're writing this conditional (which isn't working) – 2e0byo Oct 04 '21 at 11:14
  • that is my jinja template, the data in context gets added there to give output. i am trying to achieve something like https://stackoverflow.com/questions/57582464/dynamic-table-rows-with-docx-template but for my usecase. and i am trying to implement same logic with sensitive data that i cannot post here :) – arya nair Oct 04 '21 at 11:17
  • the tr tag is the template...i am trying to post it but stackoverflow is only letting me add it as an image link :( – arya nair Oct 04 '21 at 11:18
  • perhaps you are trying to post it as an image? Post it as *code* – 2e0byo Oct 04 '21 at 11:18

1 Answers1

0

Since I cannot see your template, as far as I understand it the question boils down to rejecting rows which don't contain 'arya' in their name. This is certainly posisble in jinja, but I'll not add a jinja solution until I've got a real template to work with.

It's also very easy in python:

context  = {1:{ name:"aryan",number:1},2:{name:"anand",number:2},3:{name:"aishwarya",number:3}}
context = {k:v for k, v in context.items() if "ary" in v["name"]}
...

I.e. just filter the dict of rows before you do anything else with it.

Incidentally, if your keys are really just numbers, advancing one at a time, a dict is just an expensive list.

Jinja / python-docx solution:

{% for row in x %}
   {%tr if "arya" in row.name %} {{ row.number }} {%tr endif %}
{% endfor %}

i.e. do the loop the way you normally would in jinja2, and use the special {%tr %} only when you need it.

I'm assuming x is somehow bound to context although I don't see that in the calling code.

2e0byo
  • 5,305
  • 1
  • 6
  • 26
  • i cannot make any changes to the dataset or any code, i have to do what i can with the template. the doc file template is a word doc with the table in the picture link – arya nair Oct 04 '21 at 11:27
  • Now that you've added the template code, see updated answer. Note that I don't have (or want) word, and I have never used python-docx---but this can be solved as a jinja problem, so we're probably alright :) – 2e0byo Oct 04 '21 at 11:40