วิธีแทรกรูปภาพลงในตาราง PowerPoint โดยใช้ Python

ตัวอย่างนี้มุ่งเน้นไปที่วิธีการ แทรกรูปภาพลงในตาราง PowerPoint โดยใช้ Python โดยจะอธิบายข้อมูลที่จำเป็นทั้งหมดเพื่อตั้งค่าสภาพแวดล้อมและโค้ดตัวอย่างการทำงานเพื่อ เพิ่มรูปภาพใน PPTX ตารางใน Python แอปพลิเคชันสามารถใช้ในสภาพแวดล้อมที่กำหนดค่าด้วย Python เช่น Linux, macOS หรือ Windows

ขั้นตอนในการแทรกรูปภาพในตาราง PowerPoint โดยใช้ Python

  1. สร้างสภาพแวดล้อมเป็น ใช้ Aspose.Slides สำหรับ Python ผ่าน .NET เพื่อแทรกรูปภาพภายในตาราง
  2. สร้างงานนำเสนอเริ่มต้นโดยใช้อินสแตนซ์ของคลาส Presentation จากนั้นเข้าถึงสไลด์แรกจากคอลเลกชันสไลด์
  3. แทรกตารางภายในด้วยจำนวนแถวและคอลัมน์ที่กำหนดไว้ล่วงหน้าภายในสไลด์ที่เลือกโดยใช้เมธอด add_table()
  4. แทรกรูปภาพภายในคอลเลกชันรูปภาพงานนำเสนอ
  5. เข้าถึงเซลล์ที่อยู่ในแถวและคอลัมน์แรกภายในตารางและตั้งค่ารูปภาพภายในนั้น
  6. บันทึกงานนำเสนอ PPTX ด้วยภาพตารางบนดิสก์

เราได้สำรวจวิธี แสดงรูปภาพในตาราง PPTX ใน Python โดยใช้ขั้นตอนข้างต้น ในขั้นต้น งานนำเสนอเริ่มต้นจะถูกสร้างขึ้นโดยใช้อินสแตนซ์ของคลาสงานนำเสนอพร้อมกับการเข้าถึงสไลด์แรกภายในคอลเลกชันสไลด์งานนำเสนอ เราจะสร้างตารางใหม่โดยใช้จำนวนแถวและคอลัมน์ที่กำหนดไว้ล่วงหน้าโดยใช้เมธอด add_table() ซึ่งตามด้วยการโหลดอิมเมจต้นฉบับจากดิสก์และแทรกลงในคอลเลกชันอิมเมจการนำเสนอ ในที่สุด เราจะตั้งค่าภาพที่เพิ่มภายในเซลล์ตารางก่อนที่จะบันทึกงานนำเสนอบนดิสก์

รหัสเพื่อแทรกรูปภาพในตาราง PowerPoint โดยใช้ Python

import aspose.pydrawing as draw
import aspose.slides as slides
#Path to the license and image file directory
filepath = "Y://Documents//KnowledgeBase//TestData//"
# Load the license in your application for creating a table with an image
slidesTableLicense = slides.License()
slidesTableLicense.set_license(filepath + "Conholdate.Total.Product.Family.lic")
#Create a Presentation object to add a table with an image
with slides.Presentation() as presTable:
# Access the first slide to add the table
slideForTable = presTable.slides[0]
#Define the rows' heights and columns' widths
dblColWidth = [50, 50, 50]
dblRowHeight = [50, 30, 32, 30]
#Add a table shape to slide
tblwithImage = slideForTable.shapes.add_table(100, 50, dblColWidth, dblRowHeight)
with open(filepath + "sample.png", "rb") as bin_file:
#Read the entire file from the disk at once
tblImageData = bin_file.read()
#Insert the image insdie the images collection of the presentation
imageForSlide = presTable.images.add_image(tblImageData)
#Access the first cells inside the first row of the table
tableCell = tblwithImage[0,0]
#Set the cell fill format to picture
tableCell.cell_format.fill_format.fill_type = slides.FillType.PICTURE
#Set the picture fill mode
tableCell.cell_format.fill_format.picture_fill_format.picture_fill_mode = slides.PictureFillMode.STRETCH
#Set the image for the selected cell inside the table
tableCell.cell_format.fill_format.picture_fill_format.picture.image = imageForSlide
#Save the presentations with table image
presTable.save(filepath + "PresWithTableImage.pptx", slides.export.SaveFormat.PPTX)
print("Done")

ตัวอย่างข้างต้นแสดงวิธี แทรกภาพตารางในสไลด์โดยใช้ Python โดยใช้อินเทอร์เฟซ API ที่ง่ายมาก cell_format ใช้เพื่อตั้งค่ารูปแบบการเติมสำหรับเซลล์ให้เป็นรูปภาพโดยใช้ตัวระบุ fill_type.PICTURE เราได้เพิ่มรูปภาพ PNG สำหรับเซลล์ตารางในตัวอย่างนี้ อย่างไรก็ตาม คุณยังสามารถเพิ่มรูปภาพประเภทอื่นๆ เช่น JPEG, BMP, EMF และ SVG สำหรับเซลล์ตารางได้อีกด้วย

ในหัวข้อนี้ เราได้แนะนำคุณเกี่ยวกับวิธีแทรกรูปภาพตารางในงานนำเสนอโดยใช้ Python หากคุณต้องการเรียนรู้เพิ่มเติมเกี่ยวกับการจัดการตารางภายใน PowerPoint โปรดดูหัวข้อ วิธีสร้างตารางใน PowerPoint โดยใช้ Python

 ไทย