python automatically generates the implementation code of ppt
Create a PPT using python and python-pptx
In this blog post, we will explore how to use the python library 'Python-pptx' to create a simple PowerPoint presentation (PPT). This library allows us to programmatically create slideshows, add text, images, tables, and custom shapes.
Install 'python-pptx'
First, make sure you have the python-pptx library installed. If it is not already installed, you can install it with the following command:
//bash pip install python-pptx
Create a PPT document
Creating a new powerpoint document is simple:
from pptx import Presentation prs = Presentation()
Add title slide
We can add a slide with a title and subtitle:
slide = prs.slides.add_slide(prs.slide_layouts[0]) title = slide.shapes.title subtitle = slide.placeholders[1] title.text = "Hello, World!" subtitle.text = "python-pptx was here!"
Add a slide with bullet points
Next, we add a slide with bullet points:
slide = prs.slides.add_slide(prs.slide_layouts[1]) title_shape = slide.shapes.title body_shape = slide.placeholders[1] title_shape.text = 'Adding a Bullet Slide' tf = body_shape.text_frame tf.text = 'Find the bullet slide layout' p = tf.add_paragraph() p.text = 'Use _TextFrame.text for first bullet' p.level = 1 p = tf.add_paragraph() p.text = 'Use _TextFrame.add_paragraph() for subsequent bullets' p.level = 2
Add text box
We can also add a text box with multiple paragraphs:
slide = prs.slides.add_slide(prs.slide_layouts[6]) txBox = slide.shapes.add_textbox(Inches(1), Inches(1), Inches(5), Inches(1)) tf = txBox.text_frame tf.text = "This is text inside a textbox" p = tf.add_paragraph() p.text = "This is a second paragraph that's bold" p.font.bold = True p = tf.add_paragraph() p.text = "This is a third paragraph that's big" p.font.size = Pt(40)
Add picture
Adding a picture to a slide is also simple:
img_path = '1.png' slide = prs.slides.add_slide(prs.slide_layouts[6]) slide.shapes.add_picture(img_path, Inches(1), Inches(1)) slide.shapes.add_picture(img_path, Inches(5), Inches(1), height=Inches(5.5))
Add a custom shape
We can add custom shapes to represent a process or step:
slide = prs.slides.add_slide(prs.slide_layouts[5]) shapes = slide.shapes shapes.title.text = 'Adding an AutoShape' left = Inches(0.93) top = Inches(3.0) width = Inches(1.75) height = Inches(1.0) shape = shapes.add_shape(MSO_SHAPE.PENTAGON, left, top, width, height) shape.text = 'Step 1' left += width - Inches(0.4) width = Inches(2.0) for n in range(2, 6): shape = shapes.add_shape(MSO_SHAPE.CHEVRON, left, top, width, height) shape.text = f'Step {n}' left += width - Inches(0.4)
Add table
Finally, we add a table:
slide = prs.slides.add_slide(prs.slide_layouts[5]) shapes = slide.shapes shapes.title.text = 'Adding a Table' table = add_table(2, 2, Inches(2.0), Inches(2.0), Inches(6.0), Inches(0.8)).table table.columns[0].width = Inches(2.0) table.columns[1].width = Inches(4.0) table.cell(0, 0).text = 'Foo' table.cell(0, 1).text = 'Bar' table.cell(1, 0).text = 'Baz' table.cell(1, 1).text = 'Qux'
Save PPT document
After all the edits are done, we save the document as test.pptx:
prs.save('test.pptx')
Effect preview
With the above steps, we can quickly create a PPT document with a title, bullet points, text boxes, pictures, custom shapes, and tables. The python-pptx library provides a wealth of features to meet most of our presentation production needs.
This is an introduction to automated processing of PPT documents using the python-pptx library. Hope this article can help you improve your work efficiency!
Related article
-
How Django migrates and manipulates databases
This article mainly introduces Django migration, operation database related knowledge, this article gives you a very detailed introduction, has a certain reference value, need friends can refer to2019-08-08 -
python Design mini-fiction website (based on Django+Bootstrap framework)
This article mainly introduces the python design mini-novel website (based on Django+Bootstrap framework), the article through the example code introduction is very detailed, for everyone's study or work has a certain reference learning value, need friends can refer to it2019-07-07 -
Python Web version speech synthesis example details
This article mainly introduces the Python Web version of speech synthesis example detailed explanation, speech synthesis technology can convert user input text into smooth and natural speech output, and can support speech speed, tone, volume Settings, so that human-computer communication is more natural, need friends can refer to2019-07-07 -
pytorch implements L2 and L1 regularization operations
This article mainly introduces pytorch to achieve L2 and L1 regularization operation, has a good reference value, I hope to help you. Let's take a look2021-03-03 -
python unichr function knowledge summary
In this article, Xiaobian gives you a summary of the knowledge points about python unichr function, interested friends can learn.2020-12-12 -
Python Json and pickle module serialization use introduction
This article introduces two serialization modules commonly used in Python: pickle serialization and json serialization. The example code in the article explains in detail, interested partners can learn2022-10-10 -
LyScript implementation of the command query function of the example code
The LyScript automation plug-in can be repackaged to read the machine code decoded by the target process from memory. So this article introduces how to achieve LyScript instruction query function, you need to refer to it2022-09-09 -
How to use Python to generate note picture details
python is now a hot degree I believe there is no need to introduce too much, the following article mainly introduces you about how to use Python to generate sticky note pictures relevant information, the article through the example code is very detailed, for everyone to learn or use python has a certain reference learning value. The friends who need to follow the Xiaobian to study together2018-07-07 -
How to prevent sql injection in Python
SQL injection is one of the more common network attack methods, it is not to use the BUG of the operating system to achieve the attack, but for the programmer's negligence in programming, through SQL statements, to achieve no account login, and even tamper with the database. The following article mainly introduces the method of preventing sql injection in Python, and the friends who need it can refer to it.2017-02-02
Latest comments