Pythonpptx庫用於建立/編輯PowerPoint(.pptx)文件。這不適用於MS Office 2003和先前的版本。
可以使用這個庫添加形狀、段落、文字和幻燈片等等。
安裝:開啟系統上的命令提示符,然後編寫以下命令:
pip install python-pptx
讓我們看看它的一些用法:在Python 3環境下運行。
小知識總結:
一個PPT檔案為presentation,基本的結構為展示檔案presentation-投影片頁slide-形狀shape組成,形狀就需要區分開,是包含文字的形狀還是不包含文字的形狀(純圖片等)。
如果是包含文字的形狀,則可以取得內部的文字框,一個文字框又可以看作是一個小的word文檔,包含段落paragraph - 文字區塊run
1、開啟PPT檔案
from pptx import Presentation# 這裡給出需要開啟的檔案路徑file_path = r'...' pptx = Presentation(file_path)
2、取得幻燈片頁
用 pptx.slides 可以取得一個列表,包括所有的幻燈片頁 slide 物件
for slide in pptx.slides: print(slide)
範例1:建立帶有標題和字幕投影片的新PowerPoint文件。
Placeholder佔位符已經完成了樣式設置,包括字體、字號、顏色等等。
在特定佔位符內輸入文字可直接轉換為特定的樣式
# 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] "^"s for slide root.cide_layouts[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. firidest page . 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 ould # implemented, you can change # 0 to 1 for different design slide.hold 11]. = " This is 2nd way" # Saving file root.save("Output.pptx") print("done")
產出:
例2:在PowerPoint中加入文字方塊。
文字的承載單位是 段落 paragraph和文字區塊 run
# import required thingsfrom 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 inBox left = topBox = width = height (left, top, 的adding Paragraphsp = tf.add_paragraph() # adding textp.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 filefile.save ('test_2.pptx') print("done")
產出:
範例3:PowerPoint(.pptx)檔案到文字(.txt)檔案轉換。
# import Presentation class # from pptx library from pptx import Presentation # creating an object ppt = Presentation("sample.x") # open file in write mode File_to_write_data = open("File_To_ ") # 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")
產出:
文字段落樣式修改:
- .add_run():新增新的文字區塊
- .line_spacing:段內行間距
- .runs :段落內的所有文字區塊
- .space_after :段後距
- .space_before :< /strong>段前距
文字文字樣式修改:
- .font.name :字型名稱
- .font.bold :是否加粗
- < strong>.font.italic :是否斜體
- .font.color :字體顏色
- .font.size:< /strong>字體大小
範例4:將影像插入PowerPoint檔案。
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 = Presentation() # Selecting blank slide blank_slide_layout Attaching slide 到 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 Inj0.shapes.1) left(img_path, left, top) left Inches(1) pic = slide.shapes.add_picture(img_path, left, top, height = height) # save file ppt.save('test_4.pptx') print("Done")
產出:
例5:將圖表加入PowerPoint文件中。
# import required classes/functions/methodfrom pptx import Presentation from pptx.chart.data import CategoryChartData from pptx.enum.chart import XL_CHART_TYPE from pptx.enum.chart import XL_CHART_TYPE from pptx.enum.chart # 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.catotract # Adding categories to chartchart_data.categoo], 'Escategories to chartchart_Ecategoegories, 'Ecategories, to chartchart_Ecategoegories, 'EHcategories, to chartchart_Ecategoegories)。 ] # Adding serieschart_data.add_series('Series 1', int(input("Enter Value:")), x, y, cx, cy = Inches(2), Inches(2), Inches(6), Inches(4.5) slide.shapes.add_chart( XL_CHART_TYPE.COLUMN_CLUSTERED, x, x, cy, chart_data ) # Saving fileppt .save('chart-Tutorial.pptx') print("done")
產出:
範例6:將表格加入PowerPoint文件中。
# importingfrom pptx import Presentation from pptx.util import Inches # create a Presentation object = Presentation() # Adding a blank slide in out pptslide = ppt6. the width ! x, y, cx, cy = Inches(2), Inches(2), Inches(4), Inches(1.5) # Adding tablesshape = slide.shapes y, cx, cy) # Saving the fileppt.save("Tabel_Tutorial.pptx") print("done")
產出:
文章為用戶上傳,僅供非商業瀏覽。發布者:Lomu,轉轉請註明出處: https://www.daogebangong.com/zh-Hant/articles/detail/Python-gan-huo-wan-zhuan-ban-gong-shi-ruan-jian-yi-PPT-hai-neng-zhe-yang-wan.html
评论列表(196条)
测试