×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Python
Posted by: tausif alam
Added: Apr 10, 2016 2:27 PM
Views: 97
mark down to pptx parser
a parser written to convert text file to ppt
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3.  
  4. import os
  5. import sys
  6. from cStringIO import StringIO
  7. from argparse import ArgumentParser
  8.  
  9. from pptx import Presentation
  10. from pptx.enum.shapes import MSO_SHAPE
  11. from pptx.dml.color import RGBColor
  12. from pptx.util import Inches, Pt
  13.  
  14.  
  15. def is_md(src, f):
  16.     """Returns true if file exists and has a Markdown extension"""
  17.     return os.path.isfile(os.path.join(src, f)) and f.endswith('.md')
  18.  
  19.  
  20. def get_pages(src):
  21.     """Returns a list of Markdown filenames in the source directory."""
  22.     return [os.path.join(src, f) for f in os.listdir(src) if is_md(src, f)]
  23.  
  24.  
  25. def build_deck(pages):
  26.     """Returns the actual PowerPoint deck."""
  27.     pres = Presentation()
  28.     blank_slidelayout = pres.slide_layouts[6]
  29.  
  30.     for page in pages:
  31.         slide = pres.slides.add_slide(blank_slidelayout)
  32.  
  33.         left = top = Inches(0)
  34.         width = Inches(10)
  35.         height = Inches(5.63)
  36.  
  37.         shape = slide.shapes.add_shape(MSO_SHAPE.RECTANGLE, left, top, width,
  38.                                        height)
  39.         shape.fill.solid()
  40.         shape.fill.fore_color.rgb = RGBColor(0, 0, 0)
  41.  
  42.         textbox = slide.shapes.add_textbox(left, top, width, height)
  43.         textframe = textbox.text_frame
  44.  
  45.         para = textframe.paragraphs[0]
  46.         run = para.add_run()
  47.         run.font.name = "Consolas"
  48.         run.font.size = Pt(7)
  49.         run.font.color.rgb = RGBColor(255, 255, 255)
  50.  
  51.         f = open(page)
  52.         run.text = f.read()
  53.         f.close()
  54.  
  55.     return pres
  56.  
  57.  
  58. if __name__ == '__main__':
  59.     parser = ArgumentParser(
  60.         description='My dorky colleague created a presentation using vimdeck.\
  61.                     The very last thing he would want to see is it as a real\
  62.                     PowerPoint deck. So here you go...',
  63.         epilog='This thing returns binary data, so you probably want to pipe\
  64.                the output to a file with a .pptx extension. Oh, and be sure\
  65.                to "pip install python-pptx" first.')
  66.     parser.add_argument('source', type=str, nargs=1, help='Path to source\
  67.        directory containing vimdeck markdown files.')
  68.     args = parser.parse_args()
  69.  
  70.     source = os.path.realpath(args.source[0])
  71.     deck = build_deck(get_pages(source))
  72.  
  73.     output = StringIO()
  74.     deck.save(output)
  75.     sys.stdout.write(output.getvalue())
  76.     output.close()
  77.     sys.exit()