Syncing your Jupyter Notebook Charts to Microsoft Word Reports

It involves the following steps:Saving the chart images from Jupyter Notebook to your desktop in code.Preparing your Word Document report, referencing the image names that you saved in your desktop in the appropriate location in your report.Loading the images into a new version of your Word Document.Saving Chart Images from Jupyter Notebook to your DesktopThe first step is to gather the images you want to load into your report by saving them from Jupyter Notebook to image files on your hard drive..I personally like following the convention of giving the plot image the same name as the plot object in your code.Your images must have unique names or else the first image will be overwritten by the second image.To stay organized, store the images in a separate folder designed specifically for the purpose of holding your report images.Repeating similar code for my other charts, I’m left with 5 chart images in my report_images folder:Preparing the Word Document Report with Image ReferencesThere is a popular Microsoft Word document package out there for Python called python-docx, which is great library for manipulating Word Documents with your Python code..To do this, the code roughly follows the following steps: load your Word document template, load the images from your image directory as a dict of InlineImage objects, render the images in the Word document, and save the loaded-image version to a new filename.Here is the code that does this:To run the code, you need to specify the Word template document, the new Word document name which will contain the images, and the directory where your images are stored.python load_images.py <template Word Doc Filename> <image-loaded Word Doc filename> <image directory name>In my case, I used the following command, which takes in the template template.docx, produced the image-loaded Word Document result.docx, and grabs the images from the folder report_images:python load_images.py template.docx result.docx report_imagesAnd voila, the image-loaded Word Document looks like this:You can find the code I used to create this guide on Github here.Originally published at www.marknagelberg.com on December 2, 2018.. More details

Leave a Reply