How to add custom tags to latest Ghostwriter 4.2.1 to replace Jinja tag with an image of a chart? #476
-
@chrismaddalena I'm struggling to find in the latest code the equivalent of the reportwriter.py _replace_and_write method. I need to add custom code that will search for a custom Jinja tag and replace it with an image of a chart. We have custom code in our repository (https://github.com/StratumSecurity/Ghostwriter/blob/v4.0.8-custom/ghostwriter/modules/reportwriter.py) that does this but I can't seem to find a method that gives us access to the python-docx paragraph variable to add the image to the run and to retrieve the chart data from the context variable (report JSON data). This is the current blocker I'm having from upgrading from v4.0.8 to v4.2.1. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I figured it out. Took multiple attempts and figuring out where to place the code. ExportDocxBase - Added custom code in the run method before the DocxTemplate render call to create the bar charts. The custom code creates a new subdocument using the word_doc variable in the ExportDocxBase class and I use my existing code to add the image to the subdocument. sd = word_doc.new_subdoc()
sd.add_picture(filepath, width=width, height=height)
...
# I then add the bar chart custom tag to the docx_context with the subdocument as the value before the render and save is called on the template
docx_context["project"][tag] = sd ExportReportBase - Has the custom bar chart tags in the map_rich_texts method base_context["project"]["chart_bar_rt"] = self.create_lazy_template("the report bar chart", "<p></p>", rich_text_context)
base_context["project"]["chart_bar_external_rt"] = self.create_lazy_template("the report bar chart", "<p></p>", rich_text_context)
base_context["project"]["chart_bar_internal_rt"] = self.create_lazy_template("the report bar chart", "<p></p>", rich_text_context) |
Beta Was this translation helpful? Give feedback.
I figured it out. Took multiple attempts and figuring out where to place the code.
ExportDocxBase - Added custom code in the run method before the DocxTemplate render call to create the bar charts. The custom code creates a new subdocument using the word_doc variable in the ExportDocxBase class and I use my existing code to add the image to the subdocument.
ExportReportBase - Has the custom bar chart tags in the map_rich_texts method