{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# MPLPPT\n",
    "`mplppt` is a simple library made from some hacky scripts I used to use to convert matplotlib figures to powerpoint figures. Which makes this a hacky library, I guess 😀."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Goal"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`mplppt` seeks to implement an alternative `savefig` function for `matplotlib` figures. This `savefig` function saves a `matplotlib` figure with a single axis to a powerpoint presentation with a single slide containing the figure. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Installation\n",
    "```bash\n",
    "pip install mplppt\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Imports"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import mplppt\n",
    "%matplotlib inline\n",
    "import numpy as np\n",
    "import matplotlib as mpl\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Supported Conversions"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`mplppt` supports [partly] conversion of the following matplotlib objects:\n",
    "* Lines [`matplotlib.lines.Line2D`]\n",
    "* Rectangles [`matplotlib.patches.Rectangle`]\n",
    "* Polygons [`matplotlib.patches.Polygon`]\n",
    "* pcolormesh [`matplotlib.collections.QuadMesh`]\n",
    "* text [`matplotlib.text.Text`]\n",
    "\n",
    "so far `mplppt` does not (yet) support (out of many other things):\n",
    "* markers (including tick marks)\n",
    "* linestyle\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Simple Example\n",
    "An example of all different conversions available for mplppt. Below we give an example of how all these objects can be combined into a single plot, which can then be exported to powerpoint:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# plot [Line2D]\n",
    "x = np.linspace(-1,5)\n",
    "y = np.sin(x)\n",
    "plt.plot(x,y,color='C1')\n",
    "\n",
    "# rectangle\n",
    "plt.gca().add_patch(mpl.patches.Rectangle((0, 0), 3, 0.5))\n",
    "\n",
    "# polygon\n",
    "plt.gca().add_patch(mpl.patches.Polygon(np.array([[5.0,1.0],[4.0,-0.2],[2.0,0.6]]), color=\"red\"))\n",
    "\n",
    "# pcolormesh\n",
    "x = np.linspace(0,1, 100)\n",
    "y = np.linspace(0,1, 100)\n",
    "X, Y = np.meshgrid(x,y)\n",
    "Z = X**2 + Y**2\n",
    "plt.pcolormesh(X,Y,Z)\n",
    "\n",
    "# text\n",
    "text = plt.text(0,0,'hello')\n",
    "\n",
    "# set limits\n",
    "plt.ylim(-0.5,1)\n",
    "\n",
    "# Save figure to pptx\n",
    "mplppt.savefig('first_example.pptx')\n",
    "\n",
    "# show figure\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Which results in a powerpoint slide which looks as follows:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![simple powerpoint export screenshot](img/slide.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Cool! What else can I do with this?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You are not bound to using matplotlib! The `mplppt` repository contains some standard powerpoint shapes that you can use. Try something like:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "ppt = mplppt.Group() # Create a new group of objects\n",
    "ppt += mplppt.Rectangle(name='rect', x=0, y=0, cx=100, cy=100, slidesize=(10,5)) # add an object to the group\n",
    "ppt.save('second_example.pptx') # export the group as a ppt slide"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Is any of this documented?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "No."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## How does this work?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The repository contains a template folder, which is nothing more than an empty powerpoint presentation which is unzipped. After making a copy of the template folder and adding some `xml` code for the shapes, the modified folder is zipped into a `.pptx` file."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Copyright"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "© Floris Laporte - MIT License"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
