Phân tích Bảng — Aspose.Note FOSS cho Python

Các bảng trong tài liệu OneNote được hiển thị dưới dạng cấu trúc ba cấp: Table → TableRow → TableCell. Mỗi ô có thể chứa RichText, Image, và các nút nội dung khác. Trang này bao quát mọi mẫu phân tích bảng được Aspose.Note FOSS hỗ trợ cho Python.


Lặp lại Bảng Cơ bản

Lấy tất cả các bảng trong tài liệu và đọc văn bản ô của chúng:

from aspose.note import Document, Table, TableRow, TableCell, RichText

doc = Document("MyNotes.one")

for table_num, table in enumerate(doc.GetChildNodes(Table), start=1):
    print(f"\nTable {table_num}: {len(table.Columns)} column(s)")
    for r, row in enumerate(table.GetChildNodes(TableRow), start=1):
        cells = row.GetChildNodes(TableCell)
        values = [
            " ".join(rt.Text for rt in cell.GetChildNodes(RichText)).strip()
            for cell in cells
        ]
        print(f"  Row {r}: {values}")

Thuộc tính Bảng

Thuộc tínhKiểuMô tả
Columnslist[TableColumn]Định nghĩa cột; mỗi TableColumn.Width (float) và .LockedWidth (bool)
IsBordersVisibleboolCó hiển thị viền bảng hay không
Tagslist[NoteTag]Các thẻ OneNote gắn vào bảng
from aspose.note import Document, Table

doc = Document("MyNotes.one")
for table in doc.GetChildNodes(Table):
    print(f"Columns: {len(table.Columns)}")
    print(f"Widths (pts): {[col.Width for col in table.Columns]}")
    print(f"Borders visible: {table.IsBordersVisible}")

Xuất Bảng ra CSV

Chuyển đổi một bảng sang định dạng CSV:

import csv
import io
from aspose.note import Document, Table, TableRow, TableCell, RichText

doc = Document("MyNotes.one")
output = io.StringIO()
writer = csv.writer(output)

for table in doc.GetChildNodes(Table):
    for row in table.GetChildNodes(TableRow):
        values = [
            " ".join(rt.Text for rt in cell.GetChildNodes(RichText)).strip()
            for cell in row.GetChildNodes(TableCell)
        ]
        writer.writerow(values)
    writer.writerow([])  # blank line between tables

print(output.getvalue())

Trích xuất Bảng Theo Trang

Giới hạn việc trích xuất bảng cho từng trang:

from aspose.note import Document, Page, Table, TableRow, TableCell, RichText

doc = Document("MyNotes.one")
for page_num, page in enumerate(doc.GetChildNodes(Page), start=1):
    tables = page.GetChildNodes(Table)
    if not tables:
        continue
    title = (
        page.Title.TitleText.Text
        if page.Title and page.Title.TitleText
        else f"Page {page_num}"
    )
    print(f"\n=== {title} ({len(tables)} table(s)) ===")
    for t, table in enumerate(tables, start=1):
        print(f"  Table {t}:")
        for row in table.GetChildNodes(TableRow):
            cells = row.GetChildNodes(TableCell)
            row_text = [
                " ".join(rt.Text for rt in cell.GetChildNodes(RichText)).strip()
                for cell in cells
            ]
            print(f"    {row_text}")

Nội dung Ô Ngoài Văn bản Thuần

Các ô bảng có thể chứa Image và các CompositeNode nội dung bên cạnh RichText:

from aspose.note import Document, Table, TableRow, TableCell, RichText, Image

doc = Document("MyNotes.one")
for table in doc.GetChildNodes(Table):
    for row in table.GetChildNodes(TableRow):
        for cell in row.GetChildNodes(TableCell):
            texts = [rt.Text for rt in cell.GetChildNodes(RichText) if rt.Text]
            images = cell.GetChildNodes(Image)
            print(f"  Cell texts: {texts}  images: {len(images)}")

Đếm và Tóm tắt Bảng

Thu thập thống kê về tất cả các bảng trong một tài liệu:

from aspose.note import Document, Table, TableRow, TableCell

doc = Document("MyNotes.one")
tables = doc.GetChildNodes(Table)
print(f"Total tables: {len(tables)}")

for i, table in enumerate(tables, start=1):
    rows = table.GetChildNodes(TableRow)
    if rows:
        cols = len(rows[0].GetChildNodes(TableCell))
    else:
        cols = 0
    print(f"  Table {i}: {len(rows)} row(s) x {cols} column(s)  widths={[col.Width for col in table.Columns]}")

Kiểm tra Thẻ trên Bảng

Bảng hỗ trợ NoteTag các mục trực tiếp:

from aspose.note import Document, Table, TagStatus

doc = Document("MyNotes.one")
for table in doc.GetChildNodes(Table):
    for tag in table.Tags:
        is_completed = tag.Status == TagStatus.Completed
        print(f"Table tag: {tag.Label}  icon={tag.Icon}  completed={is_completed}")

Vị trí DOM của Bảng

Bảng xuất hiện như là con của OutlineElement các nút trong Outline các container trên mỗi Page. Cấu trúc phân cấp là:

Page
  └── Outline
        └── OutlineElement
              └── Table
                    └── TableRow
                          └── TableCell
                                └── RichText / Image

Bạn cũng có thể truy cập các bảng qua GetChildNodes(Table) ở bất kỳ mức nút tổ tiên nào, và nó sẽ tìm kiếm toàn bộ cây con.


Mẹo

  • table.Columns là một danh sách của TableColumn đối tượng; sử dụng [col.Width for col in table.Columns] để lấy độ rộng cột tính bằng điểm. Độ dài bằng số cột.
  • Nội dung ô không phải lúc nào cũng hoàn toàn là RichText; luôn kiểm tra Image các nút nữa nếu độ trung thực đầy đủ là quan trọng.
  • Sử dụng table.GetChildNodes(TableRow) thay vì lặp lại for row in table nếu bạn cần một danh sách có kiểu thay vì một iterator chung.
  • IsBordersVisible phản ánh tùy chọn hiển thị của người dùng OneNote tại thời điểm lưu; nó không ảnh hưởng đến việc trích xuất nội dung.

Xem Thêm

 Tiếng Việt