I am generating a word file with pypandoc, I save it in a variable called output and then I make a response where I send that variable so that the document is downloaded
logo = str(settings.BASE_DIR) + '/cv_api/templates/logoutpl.png'
context = {'listaFinal': listaFinal, 'docente': docente, 'logo': logo}
html_string = render_to_string('documento.html', context)
output = pypandoc.convert(source= html_string, format='html', to='docx',
outputfile=str(Path.home() / "documento") + 'cv.docx', extra_args='--css=/templates/pdf_gen.css')
response = HttpResponse( output, content_type='application/msword')
response['Content-Disposition'] = 'attachment; filename="cv.docx"'
return response
Here I do the response response = HttpResponse( output, content_type='application/msword')
, but the document is saved in the place where it is indicated in the outputfile outputfile=str(Path.home() / "documento") + 'cv.docx'
In this directory I download the document with the data, but in the response I download an empty word document, how can I do so that in the response I downloaded the document that is generated in the output variable. I hope you can help me. Thanks in advance
In case it helps someone to solve this problem, I had to change the use of
pypandoc
a called librarypython-docx-template
where the document is generated based on a template,.docx
so I did it in the following way:This allowed me to generate the document with the necessary information.