-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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
[Proposal] Word templates #14095
Comments
Implementation draft for server-side: from six import BytesIO
from docxtpl import DocxTemplate
def _fill_template(template, data):
"""
Fill a word template with data.
Makes use of BytesIO to write the resulting file to memory instead of disk.
:param template: path to docx file or file-like object
:param data: dict with keys and values
"""
doc = DocxTemplate(template)
doc.render(data)
_file = BytesIO()
doc.docx.save(_file)
return _file
@frappe.whitelist()
def fill_and_attach_template(doctype, name, template):
"""
Use a documents data to fill a docx template and attach the result.
Reads a document and a template file, fills the template with data from the
document and attaches the resulting file to the document.
:param doctype" data doctype
:param name" data name
:param template" name of the template file
"""
data = frappe.get_doc(doctype, name)
data_dict = data.as_dict()
template_doc = frappe.get_doc("File", template)
template_path = template_doc.get_full_path()
output_file = _fill_template(template_path, data_dict)
output_doc = frappe.get_doc({
"doctype": "File",
"file_name": "-".join([name, template_doc.file_name]),
"attached_to_doctype": doctype,
"attached_to_name": name,
"content": output_file.getvalue(),
})
output_doc.save() Client side: var dialog = new frappe.ui.Dialog({
title: __('Choose a template file'),
fields: [
{
fieldname: 'template',
fieldtype: 'Link',
label: 'File',
options: 'File'
},
],
primary_action: () => {
dialog.hide();
frappe.call({
method: "path.to.fill_and_attach_template",
args: {
doctype: frm.doc.doctype,
name: frm.doc.name,
template: dialog.get_values().template,
},
callback: (r) => frm.reload_doc(),
});
}
});
dialog.show(); |
Check with @karthikeyan5 |
@barredterra I have a small ques, how we can deal with the child table. |
Is this feature implemented now? |
@alkuhlani child tables should already work with the example code. @CraftCoderr this has not been contributed to ERPNext yet. We have no motivation right now, but feel free to get it implemented by anyone, based on the example code. Implementation in ERPNext needs a bit of design work. I think Rushabh suggested that Word templates could be an extension of Print Format. Or you can make a custom app (quick and dirty). |
Transferred to frappe because Print Format belongs to the framework, not the ERP. |
this is a good propose just i have question since docxtpl is use jinja while frappe use jinja is this could make conflict since the different dependency could make conflict or it is normal |
True, if the Jinja versions are incompatible, it will not work. |
Fill word templates with values from any document.
Add a new DocType Word Template with fields Title and related DocType.
Create a new Template
In the related DocType, a Button "Fill Word Template" will appear.
Fill a template:
{{ var }}
in the template will be replaced with field values from the documenthttps://github.com/elapouya/python-docx-template
The text was updated successfully, but these errors were encountered: