Commits

Takayuki KONDO  committed 35295f3

bundle pptshape 0.0.2 extension

  • Participants
  • Parent commits 19a597e

Comments (0)

Files changed (12)

 
 # Add any Sphinx extension module names here, as strings. They can be extensions
 # coming with Sphinx (named 'sphinx.ext.*') or your custom ones.
-extensions = ['sphinxcontrib.plantuml', 'sphinx.ext.todo']
+extensions = ['sphinxcontrib.plantuml',
+              'sphinx.ext.todo',
+              'pptshape.directive']
+
 todo_include_todos = True
 
 plantuml = ['java', '-jar', 'ext/sphinxcontrib/plantuml.jar']

File ext/pptshape/README.rst

+pptshape
+============================
+
+Pptshape extracts shapes from ppt file and embeds to the sphinx document as png file.
+
+pptshape adds new directive as follow.
+
+::
+
+   .. ppt-shape:: abc.png
+      :pptfilename: testppt.pptx
+      :shapename: shape-title
+
+First line of the directive specifies filename of image file to be generated. `:pptfilename:` specifies 
+name of PowerPoint presentaion. `:shapename:` specifies name of shape you specified(see Usage).
+
+When you build sphinx project on Windows box with PowerPoint installed, pptshape opens ppt file 
+and create png file if ppt file is newer than image files.
+
+If the project is build on PC PowerPoint is not installed or non-Windows box, ppt-shape directive 
+behave just like as standard image directive.
+
+The ppt-shape directive is derived from standard image directive, so you can use directives such as 
+`:height:` or `:alt:` image directive has.
+
+
+Usage
+--------------------
+
+1. Install `pywin32 <http://sourceforge.net/projects/pywin32/>`_ package to your Windows box.
+
+2. Create new presentation(.ppt) and draw shape.
+
+3. Select the shape you wrote and display format tab. Specify shape of name the shape at 'title' field.
+
+4. Save presentation file.
+
+5. In `conf.py` file of your Sphinx project, add following configuration.
+
+   .. code-block:: python
+
+      extensions = ['pptshape.directive']
+
+6. Add following lines in your .rst files.
+
+   ::
+
+      .. ppt-shape:: abc.png
+         :pptfilename: testppt.pptx
+         :shapename: shape-title
+
+
+   `abc.png` is a name of png file to be created. `:pptfilename:` specifies name of PowerPoint presentaion. `:shapename:` specifies name of shape you specified at step 3.
+
+7. Build sphinx project.
+
+Requirements
+============
+
+* Python 2.7/3.3 or later
+
+* pywin32 to generate png file.
+
+Copyright 
+=========================
+
+Copyright (c) 2013, 2014 Atsuo Ishimoto
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.

File ext/pptshape/pptshape/__init__.py

Empty file added.

File ext/pptshape/pptshape/directive.py

+import os
+from docutils.parsers.rst import directives
+from docutils.parsers.rst.directives import images
+try:
+    from . import ppt
+except ImportError:
+    ppt = None
+
+class PPTShape(images.Image):
+    option_spec = images.Image.option_spec.copy()
+    option_spec['pptfilename'] = directives.unchanged_required
+    option_spec['shapename'] = directives.unchanged_required
+
+    def run(self):
+        if ppt:
+            dirname = os.path.split(self.state.document.current_source)[0]
+            filename = os.path.join(dirname, self.options['pptfilename'])
+            shapename = self.options['shapename']
+            imagename = os.path.join(dirname, self.arguments[0])
+
+            # check if ppt was updated.
+            mtime_img = 0
+            if os.path.exists(imagename):
+                mtime_img = os.stat(imagename).st_mtime
+            # TODO: How to emit error message?
+            if os.stat(filename).st_mtime > mtime_img:  
+                pptfile = ppt.open(filename)
+                if pptfile:
+                    pptfile.saveShape(shapename, imagename)
+                pptfile.quit()
+
+        return super(PPTShape, self).run()
+
+
+def setup(app):
+    app.add_directive('ppt-shape', PPTShape)

File ext/pptshape/pptshape/ppt.py

+import pywintypes
+import win32com.client, win32com.client.gencache, win32com.gen_py
+
+def open(filename):
+    try:
+        return PPTShape(filename)
+    except pywintypes.com_error as exc:
+        if exc.hresult == -2147221005: # Invalid prog-id
+            return None
+        raise
+
+class PPTShape:
+    def __init__(self, filename):
+        self.ppt = win32com.client.gencache.EnsureDispatch("PowerPoint.Application") 
+        #self.ppt.Visible = 0
+        self.filename = filename
+        self.presentation = self.ppt.Presentations.Open(self.filename)
+
+    def quit(self):
+        self.ppt.Quit()
+        self.ppt = None
+
+    def shapes(self):
+        for slide in self.presentation.Slides:
+            for shape in slide.Shapes:
+                yield shape
+
+    def findShape(self, name):
+        for shape in self.shapes():
+            if shape.Title == name:
+                return shape
+    
+    def saveShape(self, name, filename):
+        shape = self.findShape(name)
+        if not shape:
+            raise ValueError(
+                    "Shape '{}' doesn't found in {}".format(name, self.filename))
+        #ppRelativeToSlide
+        #ppClipRelativeToSlide
+        #ppScaleToFit
+        #ppScaleXY
+        if shape:
+            SCALE = 4   # Expand size by 4 to texts to be anti-aliased.
+
+            # ScaleWidth and ScaleHeight are dimentions of slide in ppScaleXY
+            # mode.
+            w = self.presentation.SlideMaster.Width 
+            h = self.presentation.SlideMaster.Height
+
+            shape.Export(filename, 
+                Filter=win32com.client.constants.ppShapeFormatPNG,
+                ScaleWidth=w*SCALE, ScaleHeight=h*SCALE,
+                ExportMode= win32com.client.constants.ppScaleXY)
+
+

File image.pptx

Binary file added.
+======
+画像集
+======
+
+爆発1
+=====
+
+.. ppt-shape:: images/bomb-black.png
+   :pptfilename: image.pptx
+   :shapename: bomb-black
+
+爆発2
+=====
+
+.. ppt-shape:: images/bomb.png
+   :pptfilename: image.pptx
+   :shapename: bomb
+   :height: 500 px
+   :width: 400 px
+   :scale: 50 %
+
+PowerPoint上で Bingった Sphinxその1
+====================================
+
+.. ppt-shape:: images/sphinx-statue.png
+   :pptfilename: image.pptx
+   :shapename: sphinx-statue
+   :scale: 50 %
+
+PowerPoint上で Bingった Sphinxその2
+====================================
+
+.. ppt-shape:: images/sphinx-real.png
+   :pptfilename: image.pptx
+   :shapename: sphinx-real
+   :height: 500 px
+   :width: 500 px

File images/bomb-black.png

Added
New image

File images/bomb.png

Added
New image

File images/sphinx-real.png

Added
New image

File images/sphinx-statue.png

Added
New image
    :numbered:
 
    uml
+   image
 
 検索とテーブル
 ==================