บทความนี้จะแนะนำ วิธีสร้างตารางใน PowerPoint โดยใช้ Python โดยจะครอบคลุมข้อมูลทั้งหมดเพื่อตั้งค่าสภาพแวดล้อม ขั้นตอนทีละขั้นตอนในการแทรกและเติมข้อมูลภายในตาราง และโค้ดตัวอย่างการทำงานที่แสดง วิธีแทรกตารางในสไลด์โดยใช้ Python นอกจากนี้ยังครอบคลุมวิธีการจัดรูปแบบข้อความภายในเซลล์ตารางและบันทึกงานนำเสนอบนดิสก์ในรูปแบบ PPT หรือ PPTX
ขั้นตอนในการสร้างตารางใน PowerPoint โดยใช้ Python
- กำหนดค่า สภาพแวดล้อมเพื่อใช้ Aspose.Slides สำหรับ Python ผ่าน .NET ในแอปพลิเคชันของคุณเพื่อเพิ่มตาราง
- สร้างงานนำเสนอเปล่าใหม่โดยใช้คลาส Presentation และเข้าถึงสไลด์เริ่มต้นแรก
- แทรกตารางในสไลด์ที่มีความกว้างของคอลัมน์และความสูงของแถวที่กำหนดไว้โดยใช้เมธอด add_table()
- สำรวจแต่ละแถวและเซลล์ตามลำดับภายในตารางที่เพิ่มใหม่
- ตั้งค่าข้อความตัวอย่างพร้อมกับคุณสมบัติเกี่ยวกับฟอนต์ในแต่ละเซลล์
- บันทึกงานนำเสนอด้วยตารางในรูปแบบ PPTX บนดิสก์
ขั้นตอนข้างต้นอธิบาย วิธีสร้างตารางใน PowerPoint โดยใช้ Python โดยในขั้นตอนแรก เราจะสร้างงานนำเสนอเริ่มต้นและเข้าถึงสไลด์แรก ในขั้นตอนต่อมา เราจะเพิ่มตารางโดยระบุพิกัดตำแหน่งสำหรับตารางพร้อมกับความกว้างของคอลัมน์และความสูงของแถว ในขั้นตอนสุดท้าย เราจะวนซ้ำทุกเซลล์ภายในตารางเพื่อตั้งค่าข้อความพร้อมกับการจัดรูปแบบตามลำดับก่อนที่จะบันทึกการนำเสนอที่เป็นผลลัพธ์ลงในดิสก์
รหัสเพื่อเพิ่มตารางใน PowerPoint โดยใช้ Python
import aspose.pydrawing as draw | |
import aspose.slides as slides | |
# Path to the license file directory | |
filepath = "Y://Documents//KnowledgeBase//TestData//" | |
# Load the license in your application for creating the table | |
slidesTableLicense = slides.License() | |
slidesTableLicense.set_license(filepath + "Conholdate.Total.Product.Family.lic") | |
# Instantiate the Presentation object to add the table | |
with slides.Presentation() as presentationTable: | |
# Access the first default slide | |
slide = presentationTable.slides[0] | |
# Define the columns widths and rows heights | |
dblColsWidth = [50, 50, 50] | |
dblRowsHeight = [50, 30, 30, 30, 30] | |
# Insert the table shape to slide | |
table = slide.shapes.add_table(100, 50, dblColsWidth, dblRowsHeight) | |
# Set the border format for each cell | |
for rowIndex in range(len(table.rows)): | |
for cellIndex in range(len(table.rows[rowIndex])): | |
table.rows[rowIndex][cellIndex].cell_format.border_top.fill_format.fill_type = slides.FillType.SOLID | |
table.rows[rowIndex][cellIndex].cell_format.border_top.fill_format.solid_fill_color.color = draw.Color.red | |
table.rows[rowIndex][cellIndex].cell_format.border_top.width = 5 | |
table.rows[rowIndex][cellIndex].cell_format.border_bottom.fill_format.fill_type = slides.FillType.SOLID | |
table.rows[rowIndex][cellIndex].cell_format.border_bottom.fill_format.solid_fill_color.color= draw.Color.red | |
table.rows[rowIndex][cellIndex].cell_format.border_bottom.width =5 | |
table.rows[rowIndex][cellIndex].cell_format.border_left.fill_format.fill_type = slides.FillType.SOLID | |
table.rows[rowIndex][cellIndex].cell_format.border_left.fill_format.solid_fill_color.color =draw.Color.red | |
table.rows[rowIndex][cellIndex].cell_format.border_left.width = 5 | |
table.rows[rowIndex][cellIndex].cell_format.border_right.fill_format.fill_type = slides.FillType.SOLID | |
table.rows[rowIndex][cellIndex].cell_format.border_right.fill_format.solid_fill_col or.color = draw.Color.red | |
table.rows[rowIndex][cellIndex].cell_format.border_right.width = 5 | |
# Merge the cells 1 and 2 of row 1 | |
table.merge_cells(table.rows[0][0], table.rows[1][1], False) | |
# Add the text inside the merged cell | |
table.rows[0][0].text_frame.text = "Merged Table Cells" | |
presentationTable.save(filepath + "NewPresentationWithTable.pptx", slides.export.SaveFormat.PPTX) | |
print("Done") |
ตัวอย่างนี้อธิบาย วิธีสร้างตารางใน PowerPoint โดยใช้ Python โดยที่อินสแตนซ์คลาสตารางใช้เพื่อแทรกตารางที่มีชุดของแถวและคอลัมน์ ออบเจกต์คลาส TextFrame ตั้งค่าข้อความ ความสูงของฟอนต์ และประเภทสัญลักษณ์แสดงหัวข้อย่อยสำหรับข้อความในย่อหน้า คุณยังสามารถใช้คุณสมบัติอื่นๆ ที่เกี่ยวข้อง เช่น ไฮไลท์ข้อความ ตั้งค่ารูปแบบการเติม เพิ่มหรือลบฟิลด์ และตั้งค่าสีไฮไลท์เพื่อบอกชื่อบางอย่าง
หัวข้อนี้ได้อธิบาย คุณจะแทรกตารางในงานนำเสนอโดยใช้ Python ได้อย่างไร หากคุณสนใจที่จะเรียนรู้เกี่ยวกับการเพิ่มภาพลายน้ำเพื่อรักษาความปลอดภัยให้กับงานนำเสนอ โปรดดูบทความใน วิธีเพิ่มลายน้ำรูปภาพใน PPTX โดยใช้ Python