Skip to content

Instantly share code, notes, and snippets.

@srinivas946
Created March 16, 2020 13:00
Show Gist options
  • Select an option

  • Save srinivas946/bb0c5ac1d0f6d0701bedc958a272da0e to your computer and use it in GitHub Desktop.

Select an option

Save srinivas946/bb0c5ac1d0f6d0701bedc958a272da0e to your computer and use it in GitHub Desktop.
Interact with Microsoft Office Power Point Using Python
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<b>List All Layouts</b>"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
" # import pptx to interact with powerpoint\n",
"from pptx import Presentation"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<pptx.slide.SlideLayout object at 0x06801420>\n",
"<pptx.slide.SlideLayout object at 0x06801930>\n",
"<pptx.slide.SlideLayout object at 0x068019F0>\n",
"<pptx.slide.SlideLayout object at 0x06801A50>\n",
"<pptx.slide.SlideLayout object at 0x06801B10>\n",
"<pptx.slide.SlideLayout object at 0x06801E40>\n",
"<pptx.slide.SlideLayout object at 0x06801F00>\n",
"<pptx.slide.SlideLayout object at 0x068015D0>\n",
"<pptx.slide.SlideLayout object at 0x068012A0>\n",
"<pptx.slide.SlideLayout object at 0x06801390>\n",
"<pptx.slide.SlideLayout object at 0x06801BA0>\n"
]
}
],
"source": [
"prs = Presentation() # create object\n",
"\n",
"# loop the available slide layouts\n",
"for slide in prs.slide_layouts: \n",
" print(slide)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<b> Add Title to the Presentation </b>"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"# import pptx to interact with powerpoint\n",
"from pptx import Presentation"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"# create object\n",
"prs = Presentation()\n",
"\n",
"# choose the first slide\n",
"slide_layout = prs.slide_layouts[0]\n",
"\n",
"# use add_slide method to add your chosen layout to slide\n",
"slide = prs.slides.add_slide(slide_layout)\n",
"\n",
"# select the title box using shapes attribute\n",
"title = slide.shapes.title\n",
"\n",
"# add text to title box\n",
"title.text = \"Hello Cybersecpy!\"\n",
"\n",
"# select the second box for subheading\n",
"subtitle = slide.placeholders[1]\n",
"\n",
"# add the text to it\n",
"subtitle.text = 'This is Python Interaction with ppt'\n",
"\n",
"# save the presentation\n",
"prs.save('test_ppt.pptx')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<b>Add Paragaph with Multiple Styles</b>"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"# import pptx to interact with powerpoint\n",
"from pptx import Presentation\n",
"from pptx.util import Inches, Pt\n",
"from pptx.dml.color import RGBColor\n",
"from pptx.enum.text import MSO_UNDERLINE"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"# create object\n",
"prs = Presentation()\n",
"\n",
"# choose the blank layout\n",
"slide_layout = prs.slide_layouts[6]\n",
"\n",
"# use add_slide method to add your chosen layout to slide\n",
"slide = prs.slides.add_slide(slide_layout)\n",
"# add text frame\n",
"left = top = width = height = Inches(1)\n",
"txt_box = slide.shapes.add_textbox(left, top, width, height)\n",
"tf = txt_box.text_frame\n",
"tf.text = \"Im inside a text box\" # add text\n",
"\n",
"# create paragraph with differnt styles\n",
"para = tf.add_paragraph()\n",
"run = para.add_run()\n",
"run.text = \"This is Paragraph with bold and underline\"\n",
"run.font.bold = True\n",
"run.font.underline = MSO_UNDERLINE.SINGLE_LINE\n",
"\n",
"para2 = tf.add_paragraph()\n",
"run2 = para2.add_run()\n",
"run2.text = \"This is second paragraph with font name and font size\"\n",
"run.font.name = 'verdana'\n",
"run.font.size = Pt(14)\n",
"\n",
"para3 = tf.add_paragraph()\n",
"run3 = para3.add_run()\n",
"run3.text = \"This is third paragrah with font color\"\n",
"run3.font.color.rgb = RGBColor(34, 100, 150)\n",
"\n",
"prs.save('font_ppt.pptx') # save presentation"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<b>Attach Image</b>"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"# import pptx to interact with powerpoint\n",
"from pptx import Presentation\n",
"from pptx.util import Inches"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# create object\n",
"prs = Presentation()\n",
"\n",
"# choose the blank layout\n",
"slide_layout = prs.slide_layouts[6]\n",
"\n",
"# use add_slide method to add your chosen layout to slide\n",
"slide = prs.slides.add_slide(slide_layout)\n",
"\n",
"# add left and top positions\n",
"left = top = Inches(1)\n",
"width = height = Inches(3)\n",
"\n",
"# image path\n",
"image_path = 'cybersecpy_logo.png'\n",
"\n",
"# add image to the slide\n",
"image = slide.shapes.add_picture(image_path, left, top, width, height)\n",
"\n",
"prs.save('image_ppt.pptx') # save presentation"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<b>Insert Table</b>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# import pptx to interact with powerpoint\n",
"from pptx import Presentation\n",
"from pptx.util import Inches"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# create object\n",
"prs = Presentation()\n",
"\n",
"# choose the blank layout\n",
"slide_layout = prs.slide_layouts[6]\n",
"\n",
"# use add_slide method to add your chosen layout to slide\n",
"slide = prs.slides.add_slide(slide_layout)\n",
"\n",
"rows = cols = 2 # add rows & columns\n",
"left = top = Inches(2.0) # add position of the table\n",
"width = Inches(6.0) # add table width\n",
"height = Inches(0.8) # add table height\n",
"\n",
"table = slide.shapes.add_table(rows, cols, left, top, width, height).table\n",
"\n",
"# set column widths\n",
"table.columns[0].width = Inches(2.0)\n",
"table.columns[1].width = Inches(4.0)\n",
"\n",
"# write column headings\n",
"table.cell(0, 0).text = 'Cell One First Row'\n",
"table.cell(0, 1).text = 'Cell Two First Row'\n",
"\n",
"# write body cells\n",
"table.cell(1, 0).text = 'Cell One Second Row'\n",
"table.cell(1, 1).text = 'Cell Two Second Row'\n",
"\n",
"prs.save('table_ppt.pptx') # save presentation"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<b>To Learn Real Time Scenarios related to PowerPoint. Refer <a href=\"https://cybersecpy.in/interact-with-microsoft-office-services-python/\">cybersecpy.in</a></b>"
]
}
],
"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.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment