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

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

Ví dụ 3: Chuyển đổi tệp PowerPoint (.pptx) sang tệp Văn bản (.txt)
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")
Output (file file_text.txt)

Ví dụ 4: Chèn hình ảnh vào tệp PowerPoint
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

Ví dụ 5: Thêm bảng vào tệp 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

Tuyệt vời ! Bạn có thể học khóa học Python cơ bản và thiết kế game với python của tôi.



from pptx import Presentation
from pptx.util import Inches
from pptx.dml.color import RGBColor
# Tạo PowerPoint mới
prs = Presentation()
def add_slide(title, content, img_path=None, color_bg=None):
slide_layout = prs.slide_layouts[1] # Title + Content
slide = prs.slides.add_slide(slide_layout)
slide.shapes.title.text = title
slide.placeholders[1].text = content
if color_bg:
slide.background.fill.solid()
slide.background.fill.fore_color.rgb = color_bg
if img_path:
slide.shapes.add_picture(img_path, Inches(5), Inches(1.5), width=Inches(3.5))
return slide
# Màu nền
color_green = RGBColor(198, 239, 206)
color_brown = RGBColor(205, 133, 63)
# Slide 1: Bìa
add_slide(“Sulfur Dioxide (SO₂) – Hóa học 11”,
“THPT Nguyễn Văn Trỗi\nGV: Đặng Thị Hương”,
img_path=”images/volcano.png”,
color_bg=color_brown)
# Slide 2: Mục tiêu
content2 = “- Kiến thức: SO₂, tác hại, biện pháp giảm thiểu\n- Kỹ năng: Tẩy màu, diệt nấm mốc, thí nghiệm minh họa\n- Thái độ: Trách nhiệm, hợp tác nhóm”
add_slide(“Mục tiêu bài học”, content2, color_bg=color_green)
# Slide 3: Engage
content3 = “Câu hỏi gợi mở: Em đã từng thấy khói từ nhà máy hay núi lửa chưa? Nó là gì?”
add_slide(“Khởi động (Engage)”, content3, img_path=”images/factory.png”, color_bg=color_green)
# Slide 4: Explore
content4 = “SO₂ trong tự nhiên và do con người tạo ra.”
add_slide(“Khám phá (Explore)”, content4, img_path=”images/volcano.png”, color_bg=color_green)
# Slide 5: Explain
content5 = “Tác hại của SO₂: ảnh hưởng sức khỏe, môi trường\nBiện pháp giảm thiểu: dùng nhiên liệu sạch, lọc khói”
add_slide(“Giải thích (Explain)”, content5, img_path=”images/factory.png”, color_bg=color_green)
# Slide 6: Elaborate
content6 = “Thí nghiệm minh họa: tẩy màu SO₂ hoặc diệt nấm mốc”
add_slide(“Mở rộng (Elaborate)”, content6, img_path=”images/experiment.png”, color_bg=color_green)
# Slide 7: Trò chơi “Cả hai”
content7 = “Câu hỏi: SO₂ từ núi lửa hay do đốt than?\nĐáp án: Cả hai\nChia nhóm thảo luận và chọn đáp án”
add_slide(“Trò chơi tương tác”, content7, color_bg=color_green)
# Slide 8: Luyện tập
content8 = “Câu hỏi trắc nghiệm nhóm:\n1. S + O₂ → ?\n2. Biện pháp giảm SO₂ hiệu quả?”
add_slide(“Luyện tập”, content8, color_bg=color_green)
# Slide 9: Củng cố
content9 = “Tóm tắt kiến thức, câu hỏi thảo luận: Em sẽ làm gì để hạn chế SO₂?”
add_slide(“Củng cố”, content9, img_path=”images/environment.png”, color_bg=color_green)
# Slide 10: Kết thúc
content10 = “Câu hỏi tự nghiên cứu: SO₂ trong không khí, mưa axit, ảnh hưởng lâu dài”
add_slide(“Kết thúc”, content10, color_bg=color_green)
# Lưu file
prs.save(“Sulfur_Dioxide_Hoa_hoc_11.pptx”)
print(“File PowerPoint đã được tạo: Sulfur_Dioxide_Hoa_hoc_11.pptx”)
# Cài thư viện cần thiết (chạy 1 lần)
!pip install python-pptx
from pptx import Presentation
from pptx.util import Inches, Pt
from pptx.dml.color import RGBColor
# Tạo PowerPoint nền tối
prs = Presentation()
# Hàm định dạng văn bản
def set_text_style(text_frame, size=28, bold=True, color=RGBColor(255, 255, 255)):
for paragraph in text_frame.paragraphs:
for run in paragraph.runs:
run.font.size = Pt(size)
run.font.bold = bold
run.font.color.rgb = color
# Trang 1: Tiêu đề
slide1 = prs.slides.add_slide(prs.slide_layouts[6])
title_box = slide1.shapes.add_textbox(Inches(1), Inches(2.5), Inches(8), Inches(2))
title_tf = title_box.text_frame
title_tf.text = “Chiếu cầu hiền – Ngô Thì Nhậm\nPhân tích phần 3”
set_text_style(title_tf, size=42, bold=True, color=RGBColor(255, 215, 0))
subtitle_box = slide1.shapes.add_textbox(Inches(2), Inches(4.2), Inches(6), Inches(1))
subtitle_tf = subtitle_box.text_frame
subtitle_tf.text = “Phong cách hiện đại – có khung chèn ảnh minh họa”
set_text_style(subtitle_tf, size=22, bold=False)
# Trang 2: Bối cảnh và khó khăn
slide2 = prs.slides.add_slide(prs.slide_layouts[6])
title2_box = slide2.shapes.add_textbox(Inches(0.5), Inches(0.5), Inches(9), Inches(1))
title2_box.text = “1. Bối cảnh và khó khăn”
set_text_style(title2_box.text_frame, size=34, color=RGBColor(255, 215, 0))
content2 = (
“🌍 Sau chiến tranh, đất nước rối ren, triều đại mới vừa được lập.\n\n”
“⚖️ Chính quyền còn non trẻ, nhiều công việc chưa ổn định.\n\n”
“🧑🎓 Nhân tài tản mác khắp nơi, chưa được trọng dụng.\n\n”
“➡️ Đây là thời điểm đất nước cần người hiền tài nhất.”
)
box2 = slide2.shapes.add_textbox(Inches(0.7), Inches(1.6), Inches(6), Inches(4))
box2.text = content2
set_text_style(box2.text_frame, size=24, bold=False)
img_note2 = slide2.shapes.add_textbox(Inches(6.8), Inches(2), Inches(3), Inches(2))
img_note2.text = “🖼️ Chèn ảnh minh họa tại đây\n(Ảnh vua Quang Trung hoặc cảnh chiến tranh)”
set_text_style(img_note2.text_frame, size=18, bold=False, color=RGBColor(200, 200, 200))
# Trang 3: Sự cần thiết
slide3 = prs.slides.add_slide(prs.slide_layouts[6])
title3_box = slide3.shapes.add_textbox(Inches(0.5), Inches(0.5), Inches(9), Inches(1))
title3_box.text = “2. Sự cần thiết phải có người hiền tài”
set_text_style(title3_box.text_frame, size=34, color=RGBColor(255, 215, 0))
content3 = (
“💡 Hiền tài là gốc rễ của quốc gia, giúp vua trị nước, an dân.\n\n”
“🏛️ Cần có người tài để khôi phục kinh tế, củng cố triều đại.\n\n”
“📜 Việc cầu hiền là nhu cầu cấp thiết, không chỉ là ý muốn cá nhân của vua.”
)
box3 = slide3.shapes.add_textbox(Inches(0.7), Inches(1.6), Inches(6), Inches(4))
box3.text = content3
set_text_style(box3.text_frame, size=24, bold=False)
img_note3 = slide3.shapes.add_textbox(Inches(6.8), Inches(2), Inches(3), Inches(2))
img_note3.text = “🖼️ Chèn ảnh minh họa tại đây\n(Ảnh hiền tài, triều đình hoặc cảnh học giả)”
set_text_style(img_note3.text_frame, size=18, bold=False, color=RGBColor(200, 200, 200))
# Trang 4: Tư tưởng và giọng văn
slide4 = prs.slides.add_slide(prs.slide_layouts[6])
title4_box = slide4.shapes.add_textbox(Inches(0.5), Inches(0.5), Inches(9), Inches(1))
title4_box.text = “3. Tư tưởng và giọng văn”
set_text_style(title4_box.text_frame, size=34, color=RGBColor(255, 215, 0))
content4 = (
“🗣️ Giọng văn chân thành, khiêm tốn, thể hiện tấm lòng cầu thị.\n\n”
“👑 Bộc lộ tầm nhìn chiến lược của vua Quang Trung.\n\n”
“🤝 Không phân biệt xuất thân, chỉ cần có tài và lòng vì nước.”
)
box4 = slide4.shapes.add_textbox(Inches(0.7), Inches(1.6), Inches(6), Inches(4))
box4.text = content4
set_text_style(box4.text_frame, size=24, bold=False)
img_note4 = slide4.shapes.add_textbox(Inches(6.8), Inches(2), Inches(3), Inches(2))
img_note4.text = “🖼️ Chèn ảnh minh họa tại đây\n(Ảnh vua Quang Trung hoặc cảnh triều đình)”
set_text_style(img_note4.text_frame, size=18, bold=False, color=RGBColor(200, 200, 200))
# Trang 5: Kết luận
slide5 = prs.slides.add_slide(prs.slide_layouts[6])
title5_box = slide5.shapes.add_textbox(Inches(0.5), Inches(0.5), Inches(9), Inches(1))
title5_box.text = “4. Kết luận”
set_text_style(title5_box.text_frame, size=34, color=RGBColor(255, 215, 0))
content5 = (
“📚 Phần 3 khẳng định: Buổi đầu lập nghiệp đầy khó khăn.\n\n”
“🌟 Hiền tài là yếu tố quyết định sự hưng thịnh của triều đại.\n\n”
“💬 Tư tưởng tiến bộ, thể hiện tinh thần trọng dụng nhân tài của vua Quang Trung.”
)
box5 = slide5.shapes.add_textbox(Inches(1), Inches(1.8), Inches(8), Inches(3.5))
box5.text = content5
set_text_style(box5.text_frame, size=26, bold=False)
note5 = slide5.shapes.add_textbox(Inches(1), Inches(5.3), Inches(8), Inches(0.8))
note5.text = “💡 Gợi ý: Thêm hiệu ứng Push giữa các slide để bài thuyết trình mượt hơn.”
set_text_style(note5.text_frame, size=18, bold=False, color=RGBColor(200, 200, 200))
# Lưu file
output_path = “Chieu_cau_hien_Hien_dai_co_khung_anh.pptx”
prs.save(output_path)
print(f”✅ Tạo xong file PowerPoint: {output_path}”)