TableRow / TableCell — Aspose.Note FOSS for Python API Reference
Lớp: TableRow
Enumerations: aspose.note Enumerations: from aspose.note import TableRow Enumerations: CompositeNode
TableRow đại diện cho một hàng duy nhất trong một Table. Các phần tử con trực tiếp của nó là TableCell nút.
Enumerations
TableRow không có thuộc tính nào ngoài những thuộc tính được kế thừa từ CompositeNode và Node.
Kế thừa từ CompositeNode
| Thuộc tính / Phương thức | Enumerations |
|---|---|
FirstChild | Phần tử con trực tiếp đầu tiên TableCell, hoặc None |
LastChild | Phần tử con trực tiếp cuối cùng TableCell, hoặc None |
AppendChildLast(node) | Thêm vào cuối một TableCell |
AppendChildFirst(node) | Thêm vào đầu một TableCell |
InsertChild(index, node) | Chèn một TableCell ở vị trí cụ thể |
RemoveChild(node) | Xóa một TableCell |
GetChildNodes(Type) | Tìm kiếm đệ quy theo chiều sâu |
for cell in row | Lặp trực tiếp TableCell các phần tử con |
Kế thừa từ Node
| Enumerations | Enumerations |
|---|---|
ParentNode | Phần chứa Table |
Document | Gốc Document |
Lớp: TableCell
Enumerations: aspose.note Enumerations: from aspose.note import TableCell Enumerations: CompositeNode
TableCell đại diện cho một ô duy nhất trong một TableRow. Nó có thể chứa RichText, Image, và các nút nội dung khác làm con.
Enumerations
TableCell không có thuộc tính nào ngoài những thuộc tính được kế thừa từ CompositeNode và Node.
Kế thừa từ CompositeNode
| Thuộc tính / Phương thức | Enumerations |
|---|---|
FirstChild | Con nội dung trực tiếp đầu tiên |
LastChild | Con nội dung trực tiếp cuối cùng |
AppendChildLast(node) | Thêm một nút nội dung vào ô này |
AppendChildFirst(node) | Thêm một nút nội dung vào đầu |
InsertChild(index, node) | Chèn vào vị trí cụ thể |
RemoveChild(node) | Xóa một nút nội dung |
GetChildNodes(Type) | Tìm kiếm đệ quy theo chiều sâu |
for child in cell | Duyệt các con trực tiếp |
Kế thừa từ Node
| Enumerations | Enumerations |
|---|---|
ParentNode | Phần chứa TableRow |
Document | Gốc Document |
Ví dụ sử dụng
Lặp qua tất cả các hàng và ô của bảng
from aspose.note import Document, Table, TableRow, TableCell, RichText
doc = Document("MyNotes.one")
for table in doc.GetChildNodes(Table):
print(f"Table column widths: {[col.Width for col in table.Columns]}")
for row in table.GetChildNodes(TableRow):
row_texts = []
for cell in row.GetChildNodes(TableCell):
texts = cell.GetChildNodes(RichText)
cell_text = " ".join(rt.Text for rt in texts if rt.Text)
row_texts.append(cell_text)
print(" |", " | ".join(row_texts), "|")Trích xuất văn bản từ mỗi ô thành một danh sách phẳng
from aspose.note import Document, TableCell, RichText
doc = Document("MyNotes.one")
for cell in doc.GetChildNodes(TableCell):
for rt in cell.GetChildNodes(RichText):
if rt.Text.strip():
print(rt.Text)Xây dựng một bảng bằng cách lập trình
from aspose.note import (
Document, Page, Outline, OutlineElement,
Table, TableRow, TableCell, RichText, SaveFormat
)
doc = Document()
page = doc.AppendChildLast(Page())
outline = page.AppendChildLast(Outline())
elem = outline.AppendChildLast(OutlineElement())
from aspose.note import TableColumn
table = elem.AppendChildLast(Table(
IsBordersVisible=True,
Columns=[TableColumn(Width=100.0), TableColumn(Width=200.0)],
))
headers = ["Name", "Value"]
row = table.AppendChildLast(TableRow())
for header_text in headers:
cell = row.AppendChildLast(TableCell())
rt = cell.AppendChildLast(RichText())
rt.Append(header_text)
data_row = table.AppendChildLast(TableRow())
for value in ["Item A", "42"]:
cell = data_row.AppendChildLast(TableCell())
rt = cell.AppendChildLast(RichText())
rt.Append(value)
doc.Save("table-output.pdf", SaveFormat.Pdf)Đếm số hàng và ô
from aspose.note import Document, Table, TableRow, TableCell
doc = Document("MyNotes.one")
for table in doc.GetChildNodes(Table):
rows = table.GetChildNodes(TableRow)
cells = table.GetChildNodes(TableCell)
print(f"Rows: {len(rows)} Cells: {len(cells)}")None-Safety
TableRow và TableCell không có thuộc tính riêng có thể null. Các danh sách con được trả về bởi GetChildNodes luôn luôn là danh sách (có thể rỗng).
Xem Thêm
- Enumerations: cha của TableRow
- RichText: nội dung chính trong TableCell
- Enumerations: nội dung hình ảnh trong TableCell
- CompositeNode: các phương thức quản lý con được kế thừa