จัดการตารางการนำเสนอใน Python
บทนำ
ตารางใน PowerPoint จัดระเบียบข้อมูลเป็นแถวและคอลัมน์ ทำให้อ่านและเปรียบเทียบค่าต่างๆ ได้ง่ายขึ้น
Aspose.Slides มีคลาส Table และ Cell รวมถึงประเภทอื่นๆ ที่ให้คุณสร้าง, ปรับปรุง, และจัดการตารางในงานนำเสนอ
สร้างตารางตั้งแต่ต้น
สร้างตารางโดยระบุตำแหน่ง, ความกว้างของคอลัมน์, และความสูงของแถว หลังจากเพิ่มลงในสไลด์แล้ว คุณสามารถจัดรูปแบบเส้นขอบของเซลล์, ผสานเซลล์, และแทรกข้อความได้
- สร้างอินสแตนซ์ของคลาส Presentation
- รับอ้างอิงถึงสไลด์ตามดัชนีของมัน
- กำหนดรายการความกว้างของคอลัมน์เป็นจุด
- กำหนดรายการความสูงของแถวเป็นจุด
- เพิ่มอ็อบเจ็กต์ Table ลงในสไลด์โดยใช้เมธอด addTable
- วนผ่านแต่ละ Cell เพื่อใช้การจัดรูปแบบกับเส้นขอบด้านบน, ด้านล่าง, ด้านขวา และด้านซ้าย
- ผสานสองเซลล์แรกของแถวแรกของตาราง
- เข้าถึงเซลล์ที่ผสานแล้วผ่านเมธอด getTextFrame
- ตั้งค่าข้อความในเซลล์ที่ผสาน
- บันทึกงานนำเสนอที่แก้ไขแล้ว
ตัวอย่างด้านล่างสร้างตารางที่มีสามคอลัมน์และห้าแถวที่ตำแหน่ง (100, 50) จุด มันใช้เส้นขอบสีแดงความกว้าง 5 จุด, ผสานสองเซลล์แรกในแถวแรก, และบันทึกผลลัพธ์เป็น table.pptx
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import FillType, Presentation, SaveFormat
from java.awt import Color
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
column_widths = [50, 50, 50]
row_heights = [50, 30, 30, 30, 30]
table = slide.getShapes().addTable(100, 50, column_widths, row_heights)
for row in table.getRows():
for cell in row:
cell_format = cell.getCellFormat()
cell_format.getBorderTop().getFillFormat().setFillType(FillType.Solid)
cell_format.getBorderTop().getFillFormat().getSolidFillColor().setColor(Color.RED)
cell_format.getBorderTop().setWidth(5)
cell_format.getBorderBottom().getFillFormat().setFillType(FillType.Solid)
cell_format.getBorderBottom().getFillFormat().getSolidFillColor().setColor(Color.RED)
cell_format.getBorderBottom().setWidth(5)
cell_format.getBorderLeft().getFillFormat().setFillType(FillType.Solid)
cell_format.getBorderLeft().getFillFormat().getSolidFillColor().setColor(Color.RED)
cell_format.getBorderLeft().setWidth(5)
cell_format.getBorderRight().getFillFormat().setFillType(FillType.Solid)
cell_format.getBorderRight().getFillFormat().getSolidFillColor().setColor(Color.RED)
cell_format.getBorderRight().setWidth(5)
table.mergeCells(table.get_Item(0, 0), table.get_Item(1, 0), False)
table.get_Item(0, 0).getTextFrame().setText("Merged Cells")
presentation.save("table.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
การนับลำดับในตารางมาตรฐาน
ในตารางมาตรฐาน ดัชนีเซลล์เริ่มจากศูนย์และใช้ลำดับ (คอลัมน์, แถว) เซลล์แรกมีดัชนีเป็น (0, 0)
ตัวอย่างเช่น เซลล์ในตารางที่มี 4 คอลัมน์และ 4 แถวถูกจัดหมายเลขดังนี้:
| (0, 0) | (1, 0) | (2, 0) | (3, 0) |
|---|---|---|---|
| (0, 1) | (1, 1) | (2, 1) | (3, 1) |
| (0, 2) | (1, 2) | (2, 2) | (3, 2) |
| (0, 3) | (1, 3) | (2, 3) | (3, 3) |
ตัวอย่างนี้สร้างตาราง 4 × 4 ตามที่แสดงด้านบน โดยมีความกว้างของคอลัมน์และความสูงของแถวเป็น 70 จุดและเส้นขอบเซลล์สีแดงความกว้าง 5 จุด พิกัดจะแสดงดัชนีเซลล์; ตัวอย่างจะปล่อยให้เซลล์ว่างไว้และบันทึกตารางเป็น StandardTables_out.pptx
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import FillType, Presentation, SaveFormat
from java.awt import Color
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
column_widths = [70, 70, 70, 70]
row_heights = [70, 70, 70, 70]
table = slide.getShapes().addTable(100, 50, column_widths, row_heights)
for row in table.getRows():
for cell in row:
cell_format = cell.getCellFormat()
cell_format.getBorderTop().getFillFormat().setFillType(FillType.Solid)
cell_format.getBorderTop().getFillFormat().getSolidFillColor().setColor(Color.RED)
cell_format.getBorderTop().setWidth(5)
cell_format.getBorderBottom().getFillFormat().setFillType(FillType.Solid)
cell_format.getBorderBottom().getFillFormat().getSolidFillColor().setColor(Color.RED)
cell_format.getBorderBottom().setWidth(5)
cell_format.getBorderLeft().getFillFormat().setFillType(FillType.Solid)
cell_format.getBorderLeft().getFillFormat().getSolidFillColor().setColor(Color.RED)
cell_format.getBorderLeft().setWidth(5)
cell_format.getBorderRight().getFillFormat().setFillType(FillType.Solid)
cell_format.getBorderRight().getFillFormat().getSolidFillColor().setColor(Color.RED)
cell_format.getBorderRight().setWidth(5)
presentation.save("StandardTables_out.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
เข้าถึงตารางที่มีอยู่แล้ว
ตารางจะถูกจัดเก็บในคอลเลกชันรูปแบบของสไลด์ ให้วนผ่านรูปแบบต่างๆ เพื่อค้นหาตาราง แล้วใช้คลาส Table เพื่ออ่านหรืออัปเดตเซลล์ของมัน
- โหลดงานนำเสนอโดยใช้คลาส Presentation
- รับอ้างอิงถึงสไลด์ที่มีตารางตามดัชนีของมัน
- วนผ่านอ็อบเจ็กต์ Shape และหยุดเมื่อพบตาราง หากสไลด์มีหลายตาราง ให้ใช้ getAlternativeText เพื่อระบุตารางที่ต้องการ
- อัปเดตข้อความในเซลล์เป้าหมาย
- บันทึกงานนำเสนอที่แก้ไขแล้ว
ตัวอย่างด้านล่างเปิดไฟล์ UpdateExistingTable.pptx และค้นหาตารางแรกในสไลด์แรก มันตั้งค่าเซลล์ที่คอลัมน์ 0, แถว 1 เป็น New และบันทึกผลลัพธ์เป็น table1_out.pptx อินพุตต้องมีอย่างน้อยหนึ่งสไลด์ และตารางแรกในสไลด์นั้นต้องมีอย่างน้อยหนึ่งคอลัมน์และสองแถว
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import Presentation, SaveFormat, Table
presentation = Presentation("UpdateExistingTable.pptx")
try:
slide = presentation.getSlides().get_Item(0)
table = None
for shape in slide.getShapes():
if isinstance(shape, Table):
table = shape
break
if table is not None:
table.get_Item(0, 1).getTextFrame().setText("New")
presentation.save("table1_out.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
เพื่อปรับขนาดแถวในตารางที่มีอยู่และเข้าใจว่าทำไมความสูงจริงจึงอาจเกินค่าต่ำสุดที่ร้องขอ, ดูที่ Control Row Height
ค้นหาเซลล์ที่เป็นเจ้าของ Text Frame
เมื่อโค้ดการประมวลผลข้อความทั่วไปได้รับอ็อบเจ็กต์ TextFrame จากตาราง ให้ใช้เมธอด TextFrame.getParentCell เพื่อดึง Cell ที่เป็นเจ้าของ สำหรับ TextFrame ของเซลล์ตาราง, TextFrame.getParentCell จะคืนค่าเจ้าของและ TextFrame.getParentShape จะคืนค่า None แม้ว่าตารางเองจะเป็นรูปแบบก็ตาม
พิกัดเซลล์สามารถเข้าถึงได้ผ่านเมธอดอ่านอย่างเดียว Cell.getFirstColumnIndex และ Cell.getFirstRowIndex TextFrame.getParentCell ยังให้การนำทางแบบอ่านอย่างเดียว: มันคืนค่าเจ้าของแต่ไม่เปลี่ยนแปลงความเป็นเจ้าของ ตรวจสอบว่าเซลล์ที่คืนค่ามาไม่ใช่ None ก่อนนำไปใช้เสมอ
สำหรับตัวอย่างครบที่ระบุเจ้าของเซลล์ตารางและรูปแบบรวมถึงรูปแบบที่เชื่อมกับโหนด SmartArt, ดูที่ Search and Replace Text
จัดตำแหน่งข้อความในตาราง
คุณสามารถควบคุมการยึดแนวตั้งและทิศทางข้อความของแต่ละเซลล์ได้ ตัวอย่างในส่วนนี้จะจัดกึ่งกลางข้อความในเซลล์แรกและหมุนข้อความ 270 องศา
- สร้างอินสแตนซ์ของคลาส Presentation
- รับอ้างอิงถึงสไลด์ตามดัชนีของมัน
- เพิ่มอ็อบเจ็กต์ Table ลงในสไลด์
- เข้าถึงอ็อบเจ็กต์ TextFrame จากตาราง
- เข้าถึง Paragraph แรกและตั้งค่าข้อความและสีของมัน
- ตั้งค่าการยึดแนวตั้งของเซลล์และทิศทางข้อความโดยใช้ setTextAnchorType และ setTextVerticalType
- บันทึกงานนำเสนอที่แก้ไขแล้ว
ตัวอย่างนี้สร้างตาราง 4 × 4 ที่มีความกว้างของคอลัมน์ 120 จุด และความสูงของแถว 100 จุด มันจัดรูปแบบข้อความในเซลล์ (0, 0), เพิ่มค่าให้กับเซลล์ที่เหลือในแถวแรก, และบันทึกผลลัพธ์เป็น Vertical_Align_Text_out.pptx
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import FillType, Presentation, SaveFormat, TextAnchorType, TextVerticalType
from java.awt import Color
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
column_widths = [120, 120, 120, 120]
row_heights = [100, 100, 100, 100]
table = slide.getShapes().addTable(100, 50, column_widths, row_heights)
table.get_Item(1, 0).getTextFrame().setText("10")
table.get_Item(2, 0).getTextFrame().setText("20")
table.get_Item(3, 0).getTextFrame().setText("30")
text_frame = table.get_Item(0, 0).getTextFrame()
paragraph = text_frame.getParagraphs().get_Item(0)
portion = paragraph.getPortions().get_Item(0)
portion.setText("Text here")
portion.getPortionFormat().getFillFormat().setFillType(FillType.Solid)
portion.getPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK)
cell = table.get_Item(0, 0)
cell.setTextAnchorType(TextAnchorType.Center)
cell.setTextVerticalType(TextVerticalType.Vertical270)
presentation.save("Vertical_Align_Text_out.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
ตั้งค่าการจัดรูปแบบข้อความในระดับตาราง
ใช้เมธอด setTextFormat เพื่อนำการจัดรูปแบบข้อความไปใช้กับทุกเซลล์ในตาราง การโอเวอร์โหลดของเมธอดรับการจัดรูปแบบส่วน, ย่อหน้า, และ TextFrame ทำให้คุณตั้งค่าคุณสมบัติเหล่านี้ได้โดยไม่ต้องวนผ่านแต่ละเซลล์
- โหลดงานนำเสนอโดยใช้คลาส Presentation
- รับอ้างอิงถึงสไลด์ตามดัชนีของมัน
- เข้าถึงอ็อบเจ็กต์ Table จากสไลด์
- ตั้งค่าขนาดฟอนต์โดยใช้ setFontHeight สำหรับข้อความ
- ตั้งค่าการจัดตำแหน่งย่อหน้าและระยะขอบขวาโดยใช้ setAlignment และ setMarginRight
- ตั้งค่าทิศทางของข้อความโดยใช้ setTextVerticalType
- บันทึกงานนำเสนอที่แก้ไขแล้ว
ตัวอย่างด้านล่างเปิดไฟล์ table.pptx ซึ่งต้องมีอย่างน้อยหนึ่งสไลด์ที่มีตารางเป็นรูปแบบแรก มันตั้งค่าขนาดฟอนต์เป็น 25 จุด, จัดย่อหน้าขวาโดยมีระยะขอบขวา 20 จุด, และทำให้ข้อความเป็นแนวตั้ง งานนำเสนอที่จัดรูปแบบแล้วบันทึกเป็น result.pptx
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import ParagraphFormat, PortionFormat, Presentation, SaveFormat, TextAlignment, TextFrameFormat, TextVerticalType, Table
presentation = Presentation("table.pptx")
try:
slide = presentation.getSlides().get_Item(0)
table = slide.getShapes().get_Item(0)
portion_format = PortionFormat()
portion_format.setFontHeight(25)
table.setTextFormat(portion_format)
paragraph_format = ParagraphFormat()
paragraph_format.setAlignment(TextAlignment.Right)
paragraph_format.setMarginRight(20)
table.setTextFormat(paragraph_format)
text_frame_format = TextFrameFormat()
text_frame_format.setTextVerticalType(TextVerticalType.Vertical)
table.setTextFormat(text_frame_format)
presentation.save("result.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
รับคุณสมบัติรูปแบบตาราง
ใช้เมธอด getStylePreset เพื่ออ่านสไตล์ตั้งล่วงหน้าของตารางและ setStylePreset เพื่อกำหนดค่า ตัวอย่างนี้ใช้ TableStylePreset.DarkStyle1 กับตารางหนึ่ง, พิมพ์ค่าพรีเซ็ต, แล้วกำหนดพรีเซ็ตเดียวกันให้กับตารางที่สอง ทั้งสองตารางบันทึกใน table-style.pptx
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import Presentation, SaveFormat, TableStylePreset
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
column_widths = [100, 150]
row_heights = [5, 5, 5]
table = slide.getShapes().addTable(10, 10, column_widths, row_heights)
table.setStylePreset(TableStylePreset.DarkStyle1)
style_preset = table.getStylePreset()
print("Table style preset: ", style_preset)
another_table = slide.getShapes().addTable(10, 100, column_widths, row_heights)
another_table.setStylePreset(style_preset)
presentation.save("table-style.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
ล็อคอัตราส่วนของตาราง
อัตราส่วนของตารางคือสัดส่วนระหว่างความกว้างและความสูงของตาราง ใช้เมธอด setAspectRatioLocked เพื่อล็อคสัดส่วนนี้สำหรับตาราง
ตัวอย่างด้านล่างเปิดไฟล์ pres.pptx ซึ่งต้องมีอย่างน้อยหนึ่งสไลด์ที่มีตารางเป็นรูปแบบแรก พิมพ์สถานะการล็อคปัจจุบัน, เปิดการล็อคอัตราส่วน, พิมพ์สถานะที่อัปเดต (True), แล้วบันทึกผลลัพธ์เป็น pres-out.pptx
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import Presentation, SaveFormat, Table
presentation = Presentation("pres.pptx")
try:
slide = presentation.getSlides().get_Item(0)
table = slide.getShapes().get_Item(0)
print("Lock aspect ratio set: ", table.getGraphicalObjectLock().getAspectRatioLocked())
table.getGraphicalObjectLock().setAspectRatioLocked(True)
print("Lock aspect ratio set: ", table.getGraphicalObjectLock().getAspectRatioLocked())
presentation.save("pres-out.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
FAQ
ฉันสามารถเปิดใช้งานการอ่านจากขวาไปซ้าย (RTL) สำหรับทั้งตารางและข้อความในเซลล์ได้หรือไม่?
ใช่ ตารางมีเมธอด setRightToLeft และย่อหน้ามี ParagraphFormat.setRightToLeft การใช้ทั้งสองวิธีจะทำให้ลำดับ RTL ถูกต้องและการแสดงผลภายในเซลล์เป็นไปตามที่คาดหวัง
ฉันจะป้องกันไม่ให้ผู้ใช้ย้ายหรือปรับขนาดตารางในไฟล์ฉบับสุดท้ายได้อย่างไร?
ใช้ shape locks เพื่อปิดการย้าย, ปรับขนาด, การเลือก ฯลฯ การล็อคเหล่านี้ใช้กับตารางได้เช่นกัน
การแทรกรูปภาพภายในเซลล์เป็นพื้นหลังได้รับการสนับสนุนหรือไม่?
ใช่ คุณสามารถตั้งค่า picture fill สำหรับเซลล์; รูปภาพจะครอบคลุมพื้นที่เซลล์ตามโหมดที่เลือก (ยืดหรือเรียงแบบกระเบื้อง)