Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rendering with merge_subdoc and then text variables. #412

Open
homerboy22 opened this issue Jan 29, 2022 · 2 comments
Open

Rendering with merge_subdoc and then text variables. #412

homerboy22 opened this issue Jan 29, 2022 · 2 comments

Comments

@homerboy22
Copy link

homerboy22 commented Jan 29, 2022

Problem:
I want to create a docx that merges subdocs based on given inputs(docx has x, y, and z sections from merged subdocs that are separate docx files), and then populates variables(name, pronoun, etc.) throughout the entire docx. I can get each to work separately(name is inserted throughout template); or subdocs are merged into template and saved. When I try and have subdocs merged, and then variables populated(so that the tags in the subdocs are filled with appropriate info) the subdocs will merge, but no tags are rendered.

How to create docx from template that merges subdocs, and then autoreplaces all input variables?

Attempted:
I have tried to add all subdocs needed and text variables into a single dictionary and then rendering. I have tried to render subdocs first, but only the 1st subdoc gets merged, and then all text variables are replaced with white space.

Code:
`for variable in ["firstName", "lastName"]:
demographics[variable] = eval(variable)

report.render(demographics)

subdocinput = report.new_subdoc('subdocfile.docx')
subdocinput2 = report.new_subdoc('subdocfile2.docx')

for tests in {"subdocinput", "subdocinput2"}:
assessments[tests] = eval(tests)
report.render(assessments)`

@htao7
Copy link

htao7 commented Mar 5, 2022

Please try to update the following packages:
docxtpl=0.15.2
lxml=4.8.0
jinja2=3.0.2

@homerboy22
Copy link
Author

Yes, using those version packages.

I figured out a way to accomplish this, but unsure if it is the most parsimonius:

  1. in base template use {{ }} jinja2 tag notation so they are escaped during first render.
  2. First render merges relevant subdocs; doc saved
  3. template object is now assigned to doc saved in step 2
  4. second render inserts demographic variables into newly assigned template doc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants