Hướng dẫn python read powerpoint - python đọc powerpoint

python-pptx là thư viện được sử dụng để tạo / chỉnh sửa tệp PowerPoint (.pptx). Chú ý rằng thư viện không hoạt động trên MS office 2003 và các phiên bản trước đó. Ta có thể thêm hình dạng, đoạn văn, văn bản và trang trình bày và nhiều thứ khác bằng cách sử dụng thư viện này.

Cài đặt: Mở Command prompt

pip install python-pptx

Hãy xem một số cách sử dụng của thư viện này:

Ví dụ 1: Tạo tệp PowerPoint mới với slide có tiêu đề và phụ đề

from pptx import Presentation      
  
root = Presentation() 
  
# Tạo slide layout
first_slide_layout = root.slide_layouts[0]  
  
""" Layout:  
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 
"""
  
# Tạo Slide mới và gán đối tượng cho biến
slide = root.slides.add_slide(first_slide_layout) 
  
# Thêm title và subtitle trong Slide
slide.shapes.title.text = " Tạo bởi python-pptx"
  
# Tạo subtitle
slide.placeholders[1].text = "Subtitle ở đây"
  
# Saving file 
root.save("Output.pptx") 
  
print("done")

Output

Hướng dẫn python read powerpoint - python đọc powerpoint

Ví dụ 2: Thêm Text-Box trong PowerPoint

from pptx import Presentation  
from pptx.util import Inches, Pt 
  
ppt = Presentation()  
  
# Tạo blank layout 
blank_slide_layout = ppt.slide_layouts[6]  
  
# Tạo slide mới 
slide = ppt.slides.add_slide(blank_slide_layout) 
  
# Margin  
left = top = width = height = Inches(1)  
  
# Tạo textBox 
txBox = slide.shapes.add_textbox(left, top, 
                                 width, height) 
  
# Tạo textFrames 
tf = txBox.text_frame 
tf.text = "Đây là văn bản bên trong textbox"
  
# Thêm Paragraphs 
p = tf.add_paragraph()  
  
# adding text 
p.text = "Đoạn thứ hai này in đậm và in nghiêng" 
  
p.font.bold = True
p.font.italic = True
  
p = tf.add_paragraph() 
p.text = "Đoạn thứ ba này font chữ lớn nhé !!!" 
p.font.size = Pt(40) 
  
# save file 
ppt.save('test_2.pptx') 
  
print("done")

Output

Hướng dẫn python read powerpoint - python đọc powerpoint

Ví dụ 2: Thêm Text-Box trong PowerPoint

from pptx import Presentation 
  
ppt = Presentation("test_2.pptx") 
  
# open file
File_to_write_data = open("file_text.txt", "w") 
  
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 + "\n") 
  
# close the file                
File_to_write_data.close() 
  
print("Done")

Ví dụ 3: Chuyển đổi tệp PowerPoint (.pptx) sang tệp Văn bản (.txt)

Hướng dẫn python read powerpoint - python đọc powerpoint

Output: file file_text.txt

from pptx import Presentation  
from pptx.util import Inches  
  
# Image path  
img_path = 'nghia.png' 
  
ppt = Presentation()  
  
blank_slide_layout = ppt.slide_layouts[6]  
  
slide = ppt.slides.add_slide(blank_slide_layout)  
  
# Margins 
left = top = Inches(1)  
  
height = Inches(3)  
  
pic = slide.shapes.add_picture(img_path, left, 
                               top, height = height) 
# save file 
ppt.save('test_4.pptx') 
  
print("Done")

Output

Hướng dẫn python read powerpoint - python đọc powerpoint

from pptx import Presentation  
from pptx.util import Inches 
  
ppt = Presentation()  
  
slide = ppt.slides.add_slide(ppt.slide_layouts[6]) 
  
# Điều chỉnh kích thước   
x, y, cx, cy = Inches(2), Inches(2), Inches(4), Inches(1.5)  
  
# Thêm Table
shape = slide.shapes.add_table(3, 4, x,  
                               y, cx, cy) 
  
# Saving 
ppt.save("test_5.pptx") 
  
print("done")

Output

Hướng dẫn python read powerpoint - python đọc powerpoint

Ví dụ 2: Thêm Text-Box trong PowerPoint

Ví dụ 3: Chuyển đổi tệp PowerPoint (.pptx) sang tệp Văn bản (.txt)