ppt pptx:Python useful information: Fun with office software (1); PPT can also be played like this-PPT tutorial免费ppt模版下载-道格办公

Python useful information: Fun with office software (1); PPT can also be played like this

Pythonpptx library for creating/editing PowerPoint(.pptx) file. This does not work on MS Office 2003 and previous versions.

Use this library to add shapes, paragraphs, text, slides, and more.

Installation: Open a command prompt on your system and write the following command:

pip install python-pptx

Let's look at some of its uses: Running in a Python 3 environment.

Little knowledge summary:

A PPT file is a presentation, and its basic structure is composed of a presentation file, a slide page, and a shape shape. The shapes need to be distinguished, whether they are shapes that contain text or shapes that do not contain text (pure pictures, etc.).

If it is a shape containing text, you can get the internal text box. A text box can be regarded as a small word document, including paragraph paragraph - text block run

1. Open the PPT file

from pptx import Presentation# Here is the file path to be opened file_path = r'...' pptx = Presentation(file_path)

2. Get the slide page

Use pptx.slides to get a list of all slide pages slide objects

for slide in pptx.slides: print(slide)

Example 1: Create a new PowerPoint file with title and subtitle slides.

PlaceholderThe placeholder has completed style settings, including font, font size, color, etc.

Entering text in a specific placeholder can be directly converted into a specific style

# import Presentation class # from pptx library from pptx import Presentation # Creating presentation object root = Presentation() # Creating slide layout first_slide_layout = root.slide_layouts[0] """ Ref for slide types: 0 -> ; title and subtitle 1 -> title and content 2 -> section header 3 -> two content 4 -> Comparison 5 -> Title only 6 -> Blank 7 -> Content with caption 8 -> Pic with caption """ # Creating slide object to add # in ppt i.e. Attaching slides # with Presentation i.e. ppt slide = root.slides.add_slide(first_slide_layout) # Adding title and subtitle in # slide i.e. first page of slide slide.shapes. title.text = " Created By python-pptx" # We have different formats of # subtitles in ppts, for simple # subtitle this method should # implemented, you can change # 0 to 1 for different design slide.placeholders[1].text = " This is 2nd way" # Saving file root.save("Output.pptx") print("done") 

Output:

Example 2: Add a text box in PowerPoint.

The carrying units of text are paragraph paragraph and text block run

# import required things from pptx import Presentation from pptx.util import Inches, Pt # Creating Objectppt = Presentation() # To create blank slide layout# We have to use 6 as an argument# of slide_layouts blank_slide_layout = ppt. slide_layouts[6] # Attaching slide obj to slideslide = ppt.slides.add_slide(blank_slide_layout) # For adjusting the Margins in inches left = top = width = height = Inches(1) # creating textBoxtxBox = slide.shapes.add_textbox(left, TOP, Width, Height)# Creating textFramestf = txbox.text_frametf.text = "this is text inside a textbox"# adding paragraphsp = tf.add_paragraph ( )# Addtextp.text = "This is a Second Paragraph That's Bold and Italic" # font p.font.bold = Truep.font.italic = True p = tf.add_paragraph()p.text = "This is a third paragraph that's big " p.font.size = Pt(40) # save fileppt.save ('test_2.pptx') print("done")

Output:


Example 3: Convert PowerPoint (.pptx) files to text (.txt) files.

# import Presentation class # from pptx library from pptx import Presentation # creating an object ppt = Presentation("sample.pptx") # open file in write mode File_to_write_data = open("File_To_Extract_ppt.txt", "w ") # write text from powerpoint # file into .txt file for slide in ppt.slides: for shape in slide.shapes: if not shape.has_text_frame: continue for paragraph in shape.text_frame.paragraphs: for run in paragraph.runs: File_to_write_data.write(run.text) # close the file File_to_write_data.close() print("Done")

Output:

Text paragraph style modification:

  • .add_run(): Add a new block of text
  • .line_spacing: Line spacing within a paragraph
  • .runs:All blocks of text within the paragraph
  • .space_after:The space after the paragraph
  • .space_before:< /strong>Paragraph spacing

Text text style modification:

  • .font.name:Font name
  • .font.bold:Whether to bold
  • < strong>.font.italic:Whether italic
  • .font.color:Font color
  • .font.size:< /strong>Font size

Example 4: Insert images into PowerPoint files.

from pptx import Presentation from pptx.util import Inches # Giving Image path img_path = 'bg_bg.png' # Creating an Presentation object ppt = Presentation() # Selecting blank slide blank_slide_layout = ppt.slide_layouts[6] # Attaching slide to ppt slide = ppt.slides.add_slide(blank_slide_layout) # For margins left = top = Inches(1) # adding images pic = slide.shapes.add_picture(img_path, left, top) left = Inches(1) height = Inches(1) pic = slide.shapes.add_picture(img_path, left, top, height = height) # save file ppt.save('test_4.pptx') print("Done")

Output:

Example 5: Add a chart to a PowerPoint file.

# import required classes/functions/method from pptx import Presentation from pptx.chart.data import CategoryChartData from pptx.enum.chart import XL_CHART_TYPE from pptx.util import Inches # Create presentation objectppt = Presentation() # Adding slide with specific layoutslide = ppt.slides.add_slide(ppt.slide_layouts[6]) # Define chart data # Creating object of chartchart_data = CategoryChartData() # Adding categories to chartchart_data.categories = ['East', 'West', 'Midwest' ]​​ int(input("Enter Value:")))) x, y, cx, cy = Inches(2), Inches(2), Inches(6), Inches(4.5) slide.shapes.add_chart( .save('chart-Tutorial.pptx') print("done")

Output:

Example 6: Add a table to a PowerPoint file.

# importingfrom pptx import Presentation from pptx.util import Inches # create a Presentation objectppt = Presentation() # Adding a blank slide in out pptslide = ppt.slides.add_slide(ppt.slide_layouts[6]) # Adjusting the width ! x, y, cx, cy = Inches(2), Inches(2), Inches(4), Inches(1.5) # Adding tablesshape = slide.shapes.add_table(3, 4, cy) # Saving the fileppt.save("Tabel_Tutorial.pptx") print("done")

Output:

Articles are uploaded by users and are for non-commercial browsing only. Posted by: Lomu, please indicate the source: https://www.daogebangong.com/en/articles/detail/Python-gan-huo-wan-zhuan-ban-gong-shi-ruan-jian-yi-PPT-hai-neng-zhe-yang-wan.html

Like (810)
Reward 支付宝扫一扫 支付宝扫一扫
single-end

Related Suggestion