{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Creating PPTX files\n", "\n", "[python-pptx](http://python-pptx.readthedocs.org/) is a Python library for manipulating [Open XML presentations](http://officeopenxml.com/anatomyofOOXML-pptx.php)\n", "\n", "## Installation\n", "\n", "Type `pip install python-pptx` or download [python-pptx binaries](https://pypi.python.org/pypi/python-pptx/0.2.0).\n", "\n", "This depends on [lxml](http://lxml.de/) and [Python Imaging Library](http://www.pythonware.com/products/pil/)\n", "\n", "## Getting started\n", "\n", "This creates a presentation with no slides and saves it as `sample.pptx`." ] }, { "cell_type": "code", "collapsed": false, "input": [ "# The shape drawing library\n", "\n", "from pptx import Presentation\n", "from lxml import etree, objectify\n", "from lxml.builder import ElementMaker\n", "\n", "_globals = {\n", " 'shape': 0,\n", "}\n", "\n", "# from pptx.shapes import _nsmap as nsmap\n", "nsmap = {\n", " 'p': 'http://schemas.openxmlformats.org/presentationml/2006/main',\n", " 'a': 'http://schemas.openxmlformats.org/drawingml/2006/main',\n", "}\n", "\n", "a = ElementMaker(namespace=nsmap['a'], nsmap=nsmap)\n", "p = ElementMaker(namespace=nsmap['p'], nsmap=nsmap)\n", "\n", "def xmlns(*prefixes):\n", " return ' '.join('xmlns:%s=\"%s\"' % (p, nsmap[p]) for p in prefixes)\n", "\n", "_shape = ''\n", " ' '\n", " ' '\n", " ' '\n", " ' '\n", " ' '\n", " ' '\n", " ' '\n", " ' '\n", " ' '\n", " ' '\n", " ' '\n", " ' '\n", " ' '\n", " ' '\n", " '')\n", "\n", "def shape(geom, x, y, w, h):\n", " \"\"\"\n", " Return a new shape object. For example:\n", " \n", " shape('ellipse', x=0, y=0, w=971600, h=971600)\n", "\n", " Popular shapes include: 'line', 'rect', 'roundRect' and 'ellipse'.\n", "\n", " Refer \n", " \"\"\"\n", " id = _globals['shape'] = _globals['shape'] + 1\n", " shp = objectify.fromstring(_shape % (id, 'Shape %d' % id, x, y, w, h, geom))\n", " # setattr(shp, 'pr', shp.find('.//p:spPr', namespaces=nsmap))\n", " return shp\n", "\n", "def color(schemeClr=None, srgbClr=None, prstClr=None, hslClr=None, sysClr=None, scrgbClr=None, **mod):\n", " \"\"\"\n", " Return a new color object.\n", " You may use any one of the following ways of specifying colour:\n", " \n", " color(schemeClr='accent2') # = second theme color\n", " color(prstClr='black') # = #000000\n", " color(hslClr=[14400000, 100.0, 50.0]) # = #000080\n", " color(sysClr='windowText') # = window text color\n", " color(scrgbClr=(50000, 50000, 50000)) # = #808080\n", " color(srgbClr='aaccff') # = #aaccff\n", " \n", " One or more of these modifiers may be specified:\n", " \n", " - alpha : '10%' indicates 10% opacity\n", " - alphaMod : '10%' increased alpha by 10% (50% becomes 55%)\n", " - alphaOff : '10%' increases alpha by 10 points (50% becomes 60%)\n", " - blue : '10%' sets the blue component to 10%\n", " - blueMod : '10%' increases blue by 10% (50% becomes 55%)\n", " - blueOff : '10%' increases blue by 10 points (50% becomes 60%)\n", " - comp : True for opposite hue on the color wheel (e.g. red -> cyan)\n", " - gamma : True for the sRGB gamma shift of the input color\n", " - gray : True for the grayscale version of the color\n", " - green : '10%' sets the green component to 10%\n", " - greenMod : '10%' increases green by 10% (50% becomes 55%)\n", " - greenOff : '10%' increases green by 10 points (50% becomes 60%)\n", " - hue : '14400000' sets the hue component to 14400000\n", " - hueMod : '600000' increases hue by 600000 (14400000 becomes 20000000)\n", " - hueOff : '10%' increases hue by 10 points (50% becomes 60%)\n", " - inv : True for the inverse color. R, G, B are all inverted\n", " - invGamma : True for the inverse sRGB gamma shift of the input color\n", " - lum : '10%' sets the luminance component to 10%\n", " - lumMod : '10%' increases luminance by 10% (50% becomes 55%)\n", " - lumOff : '10%' increases luminance by 10 points (50% becomes 60%)\n", " - red : '10%' sets the red component to 10%\n", " - redMod : '10%' increases red by 10% (50% becomes 55%)\n", " - redOff : '10%' increases red by 10 points (50% becomes 60%)\n", " - sat : '100000' sets the saturation component to 100%\n", " - satMod : '10%' increases saturation by 10% (50% becomes 55%)\n", " - satOff : '10%' increases saturation by 10 points (50% becomes 60%)\n", " - shade : '10%' is 10% of input color, 90% black\n", " - tint : '10%' is 10% of input color, 90% white\n", " \n", " Refer \n", " \"\"\"\n", " ns = xmlns('a')\n", " if schemeClr:\n", " s = '' % (ns, schemeClr)\n", " elif srgbClr:\n", " s = '' % (ns, srgbClr)\n", " elif prstClr:\n", " s = '' % (ns, prstClr)\n", " elif hslClr:\n", " s = '' % ((ns,) + tuple(hslClr))\n", " elif sysClr:\n", " s = '' % (ns, sysClr)\n", " elif scrgbClr:\n", " s = '' % ((ns,) + tuple(scrgbClr))\n", " color = objectify.fromstring(s)\n", " for arg, val in mod.iteritems():\n", " if val is True:\n", " color.append(etree.fromstring('' % (arg, ns)))\n", " else:\n", " color.append(etree.fromstring('' % (arg, ns, val)))\n", " return color\n" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "from pptx import Presentation\n", "\n", "# Create an empty presentation with no slides\n", "prs = Presentation()\n", "\n", "# Add a slide with the slide layout #6.\n", "# Some other layouts are:\n", "# 0 - Title slide\n", "# 1 - Title and Content\n", "# 2 - Section Header\n", "# ... etc (in PowerPoint, click the New Slide dropdown to see the layouts)\n", "slide = prs.slides.add_slide(prs.slidelayouts[6])" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [ "shp = shape('ellipse', 0, 0, 999999, 999999)\n", "shapes = slide._element.find('.//p:spTree', namespaces=nsmap)\n", "shapes.append(shp)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [ "# Fill with a scheme colour\n", "shp.spPr.append(a.solidFill(color(\n", " schemeClr='accent2', # 2nd theme colour\n", " tint='50%', # 50% white mixed\n", " alpha='30%' # 30% opaque, 70% transparent\n", ")))\n", "\n", "shp = shape('ellipse', 999999, 0, 999999, 999999)\n", "shapes.append(shp)\n", "\n", "# Fill with an RGB colour\n", "shp.spPr.append(a.solidFill(color(\n", " srgbClr='FF0000', # Red\n", " shade='50%', # 50% black mixed\n", " sat='30%' # 30% saturation\n", ")))\n", "\n", "shp = shape('ellipse', 0, 999999, 999999, 999999)\n", "shapes.append(shp)\n", "\n", "# Fill with an RGB colour\n", "shp.spPr.append(a.gradFill(\n", " a.gsLst(\n", " a.gs(color(schemeClr='accent2', tint= '0%'), pos=\"0\"),\n", " a.gs(color(schemeClr='accent2', tint='20%'), pos=\"50000\"),\n", " a.gs(color(schemeClr='accent2', tint='40%'), pos=\"100000\"),\n", " ),\n", " a.lin(ang='2700000', scaled='1'), # out of 21600000 = 1/8 = 45 degrees\n", "))\n", "\n", "# Add a line\n", "shp.spPr.append(a.ln(\n", " a.solidFill(color( # Solid fill with\n", " schemeClr='accent2', # 2nd theme colour\n", " shade='20%', # 20% black mixed\n", " alpha='50%', # 50% transparent\n", " )),\n", " w='3175', # 0.25pt stroke width\n", "))\n" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 4 }, { "cell_type": "code", "collapsed": false, "input": [ "# Add text\n", "shp.append(p.txBody(\n", " a.bodyPr(anchor='ctr'), # vertically center the text\n", " a.p(\n", " a.pPr(algn='ctr'), # horizontally center the text\n", " a.r(a.t('abc')),\n", ")))" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 6 }, { "cell_type": "code", "collapsed": false, "input": [ "prs.save('sample.pptx')" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 7 } ], "metadata": {} } ] }