Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 1st, 2012  |  syntax: None  |  size: 2.74 KB  |  hits: 18  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. generating a Powerpoint file using Python
  2. import os,sys,getopt,struct
  3. from cStringIO import StringIO
  4. from odf.opendocument import OpenDocumentPresentation
  5. from odf.style import Style, MasterPage, PageLayout, PageLayoutProperties,
  6. TextProperties, GraphicProperties, ParagraphProperties, DrawingPageProperties
  7. from odf.text import P
  8. from odf.draw  import Page, Frame, TextBox, Image
  9.  
  10. # also defined getImageData function that returns content_type, width, height of image.
  11.  
  12. outputfile = "photoalbum.odp"
  13.  
  14. doc = OpenDocumentPresentation()
  15.  
  16. # We must describe the dimensions of the page
  17. pagelayout = PageLayout(name="MyLayout")
  18. doc.automaticstyles.addElement(pagelayout)
  19. pagelayout.addElement(PageLayoutProperties(margin="0pt", pagewidth="800pt",
  20.     pageheight="600pt", printorientation="landscape"))
  21.  
  22. # Style for the title frame of the page
  23. # We set a centered 34pt font with yellowish background
  24. titlestyle = Style(name="MyMaster-title", family="presentation")
  25. titlestyle.addElement(ParagraphProperties(textalign="center"))
  26. titlestyle.addElement(TextProperties(fontsize="34pt"))
  27. titlestyle.addElement(GraphicProperties(fillcolor="#ffff99"))
  28. doc.styles.addElement(titlestyle)
  29.  
  30. # Style for the photo frame
  31. photostyle = Style(name="MyMaster-photo", family="presentation")
  32. doc.styles.addElement(photostyle)
  33.  
  34. # Create automatic transition
  35. dpstyle = Style(name="dp1", family="drawing-page")
  36. dpstyle.addElement(DrawingPageProperties(transitiontype="automatic",
  37.    transitionstyle="move-from-top", duration="PT5S"))
  38. doc.automaticstyles.addElement(dpstyle)
  39.  
  40. # Every drawing page must have a master page assigned to it.
  41. masterpage = MasterPage(name="MyMaster", pagelayoutname=pagelayout)
  42. doc.masterstyles.addElement(masterpage)
  43.  
  44. if len(args) == 0:
  45.     pict_dir = "."
  46. else:
  47.     pict_dir = args[0]
  48. # Slides
  49. for picture in os.listdir(pict_dir):
  50.     try:
  51.         pictdata = open(pict_dir + "/" + picture).read()
  52.     except:
  53.         continue
  54.     ct,w,h = getImageInfo(pictdata) # Get dimensions in pixels
  55.     if ct != 'image/jpeg':
  56.         continue
  57.     if w > 720:
  58.        h = float(h) * 720.0 / float(w)
  59.        w = 720.0
  60.     if h > 540.0:
  61.        w = float(w) * 540.0 / float(h)
  62.        h = 540.0
  63.  
  64.     page = Page(stylename=dpstyle, masterpagename=masterpage)
  65.     doc.presentation.addElement(page)
  66.     titleframe = Frame(stylename=titlestyle, width="720pt", height="56pt", x="40pt", y="10pt")
  67.     page.addElement(titleframe)
  68.     textbox = TextBox()
  69.     titleframe.addElement(textbox)
  70.     textbox.addElement(P(text=picture))
  71.  
  72.     offsetx = 400.0 - w/2.0
  73.     photoframe = Frame(stylename=photostyle, width="%fpt" % w, height="%fpt" % h, x="%fpt" % offsetx, y="56pt")
  74.     page.addElement(photoframe)
  75.     href = doc.addPicture(pict_dir + "/" + picture)
  76.     photoframe.addElement(Image(href=href))
  77.  
  78. doc.save(outputfile)