วิธีทำงานกับตารางใน Python
Aspose.Slides FOSS for Python supports creating tables on slides with configurable column widths and row heights. This guide shows how to add a table, populate it with data, and apply basic text formatting to cells.
คู่มือแบบขั้นตอนต่อขั้นตอน
ขั้นตอนที่ 1: ติดตั้งแพ็กเกจ
pip install aspose-slides-fossขั้นตอนที่ 2: สร้างหรือเปิดงานนำเสนอ
import aspose.slides_foss as slides
from aspose.slides_foss.export import SaveFormat
with slides.Presentation() as prs:
slide = prs.slides[0]
# ... add table ...
prs.save("table.pptx", SaveFormat.PPTX)ขั้นตอนที่ 3: กำหนดความกว้างของคอลัมน์และความสูงของแถว
ตารางต้องการความกว้างของคอลัมน์และความสูงของแถวที่ระบุเป็นจุด (1 point = 1/72 นิ้ว) สไลด์มาตรฐานมีความกว้าง 720 จุดและความสูง 540 จุด.
col_widths = [200.0, 150.0, 150.0] # 3 columns: 200pt + 150pt + 150pt
row_heights = [45.0, 40.0, 40.0] # 3 rows: 45pt header + 40pt data rowsขั้นตอนที่ 4: เพิ่มตาราง
slide.shapes.add_table(x, y, col_widths, row_heights) สร้างตารางที่ตำแหน่ง (x, y):
import aspose.slides_foss as slides
from aspose.slides_foss.export import SaveFormat
with slides.Presentation() as prs:
slide = prs.slides[0]
col_widths = [200.0, 150.0, 150.0]
row_heights = [45.0, 40.0, 40.0]
table = slide.shapes.add_table(50, 100, col_widths, row_heights)
prs.save("table.pptx", SaveFormat.PPTX)ขั้นตอนที่ 5: ตั้งค่าข้อความในเซลล์
เข้าถึงเซลล์ผ่าน table.rows[row_index][col_index] และกำหนดข้อความผ่าน .text_frame.text:
import aspose.slides_foss as slides
from aspose.slides_foss.export import SaveFormat
with slides.Presentation() as prs:
slide = prs.slides[0]
col_widths = [200.0, 150.0, 150.0]
row_heights = [45.0, 40.0, 40.0]
table = slide.shapes.add_table(50, 100, col_widths, row_heights)
# Header row (row 0)
headers = ["Product", "Units Sold", "Revenue"]
for col, header in enumerate(headers):
table.rows[0][col].text_frame.text = header
# Data rows
data = [
["Widget A", "1,200", "$24,000"],
["Widget B", "850", "$17,000"],
]
for row_idx, row_data in enumerate(data):
for col, cell_text in enumerate(row_data):
table.rows[row_idx + 1][col].text_frame.text = cell_text
prs.save("sales-table.pptx", SaveFormat.PPTX)ขั้นตอนที่ 6: จัดรูปแบบข้อความในเซลล์หัวตาราง
ใช้การจัดรูปแบบตัวหนากับเซลล์หัวตารางโดยใช้ PortionFormat:
from aspose.slides_foss import NullableBool, FillType
from aspose.slides_foss.drawing import Color
for col in range(len(headers)):
cell = table.rows[0][col]
portions = cell.text_frame.paragraphs[0].portions
if portions:
fmt = portions[0].portion_format
fmt.font_bold = NullableBool.TRUE
fmt.fill_format.fill_type = FillType.SOLID
fmt.fill_format.solid_fill_color.color = Color.from_argb(255, 255, 255, 255)ตัวอย่างทำงานครบถ้วน
import aspose.slides_foss as slides
from aspose.slides_foss import NullableBool, FillType
from aspose.slides_foss.drawing import Color
from aspose.slides_foss.export import SaveFormat
data_rows = [
["North", "$1.2M", "+8%"],
["South", "$0.9M", "+4%"],
["East", "$1.5M", "+12%"],
["West", "$0.7M", "+2%"],
]
headers = ["Region", "Revenue", "Growth"]
with slides.Presentation() as prs:
slide = prs.slides[0]
col_widths = [180.0, 140.0, 120.0]
row_heights = [45.0] + [38.0] * len(data_rows)
table = slide.shapes.add_table(60, 80, col_widths, row_heights)
# Header row
for col, text in enumerate(headers):
cell = table.rows[0][col]
cell.text_frame.text = text
if cell.text_frame.paragraphs and cell.text_frame.paragraphs[0].portions:
fmt = cell.text_frame.paragraphs[0].portions[0].portion_format
fmt.font_bold = NullableBool.TRUE
# Data rows
for row_idx, row_data in enumerate(data_rows):
for col, text in enumerate(row_data):
table.rows[row_idx + 1][col].text_frame.text = text
prs.save("regional-revenue.pptx", SaveFormat.PPTX)
print("Saved regional-revenue.pptx")ปัญหาทั่วไปและวิธีแก้
IndexError เมื่อเข้าถึง table.rows[row][col]
ดัชนีแถวและคอลัมน์เริ่มจากศูนย์ หากคุณกำหนด row_heights โดยมี 3 องค์ประกอบ ดัชนีแถวที่ถูกต้องคือ 0, 1, 2.
ข้อความในเซลล์ไม่แสดงในไฟล์ที่บันทึก
ควรกำหนดค่าผ่าน .text_frame.text, ไม่ใช่ผ่าน .text โดยตรงบนอ็อบเจ็กต์เซลล์:
# Correct
table.rows[0][0].text_frame.text = "Header"
# Wrong: AttributeError or silent failure
table.rows[0][0].text = "Header"Table position is off the slide: ตำแหน่งของตารางอยู่นอกสไลด์
ตรวจสอบว่า x + sum(col_widths) <= 720 และ y + sum(row_heights) <= 540 สำหรับสไลด์มาตรฐาน.
คำถามที่พบบ่อย
Can I merge table cells?: ฉันสามารถรวมเซลล์ตารางได้หรือไม่?
การรวมเซลล์ไม่รองรับในรุ่นนี้.
ฉันสามารถกำหนดสีพื้นหลังทั่วทั้งตารางได้หรือไม่?
ใช้การจัดรูปแบบการเติมสีกับแต่ละเซลล์แยกกัน:
for row_idx in range(len(table.rows)):
for col_idx in range(len(table.rows[row_idx])):
cell = table.rows[row_idx][col_idx]
cell.fill_format.fill_type = FillType.SOLID
cell.fill_format.solid_fill_color.color = Color.from_argb(255, 240, 248, 255)ฉันสามารถตั้งค่าสไตล์เส้นขอบของเซลล์ได้หรือไม่?
คุณสมบัติขอบของเซลล์สามารถเข้าถึงได้ผ่าน table.rows[row][col].border_* properties. ดูเอกสารอ้างอิง API เพื่อรับรายการเต็มของแอตทริบิวต์การจัดรูปแบบขอบ.