Phân tích bảng OneNote trong Python

Microsoft OneNote cho phép người dùng nhúng các bảng có cấu trúc trực tiếp vào các trang, rất phù hợp cho danh sách công việc, lịch trình, ma trận so sánh và biểu mẫu thu thập dữ liệu. Aspose.Note FOSS cho Python cho phép trích xuất tất cả dữ liệu dạng bảng này một cách lập trình, mà không cần cài đặt Microsoft Office.

Cài đặt

pip install aspose-note

Tải tài liệu và tìm các bảng

GetChildNodes(Table) thực hiện tìm kiếm đệ quy trên toàn bộ tài liệu và trả về mọi bảng dưới dạng một Table đối tượng:

from aspose.note import Document, Table

doc = Document("MyNotes.one")
tables = doc.GetChildNodes(Table)
print(f"Found {len(tables)} table(s)")

Đọc giá trị ô

Các bảng tuân theo cấu trúc ba cấp độ: Table → TableRow → TableCell. Mỗi ô chứa RichText các nút mà .Text cung cấp nội dung văn bản thuần:

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

doc = Document("MyNotes.one")

for t_num, table in enumerate(doc.GetChildNodes(Table), start=1):
    print(f"\nTable {t_num}:")
    for r_num, row in enumerate(table.GetChildNodes(TableRow), start=1):
        cells = row.GetChildNodes(TableCell)
        row_values = [
            " ".join(rt.Text for rt in cell.GetChildNodes(RichText)).strip()
            for cell in cells
        ]
        print(f"  Row {r_num}: {row_values}")

Kiểm tra độ rộng cột

Table.ColumnWidths trả về độ rộng đã lưu của mỗi cột tính bằng điểm:

from aspose.note import Document, Table

doc = Document("MyNotes.one")
for i, table in enumerate(doc.GetChildNodes(Table), start=1):
    print(f"Table {i}: {len(table.ColumnWidths)} column(s)")
    print(f"  Widths (pts): {table.ColumnWidths}")
    print(f"  Borders visible: {table.BordersVisible}")

Xuất tất cả các bảng sang CSV

Chuyển đổi mỗi bảng trong tài liệu sang định dạng CSV:

import csv, 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 row between tables

with open("tables.csv", "w", encoding="utf-8", newline="") as f:
    f.write(output.getvalue())

print("Saved tables.csv")

Xuất các bảng sang một Dict / JSON của Python

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

doc = Document("MyNotes.one")
result = []

for table in doc.GetChildNodes(Table):
    rows = []
    for row in table.GetChildNodes(TableRow):
        cells = [
            " ".join(rt.Text for rt in cell.GetChildNodes(RichText)).strip()
            for cell in row.GetChildNodes(TableCell)
        ]
        rows.append(cells)
    result.append({"rows": rows, "column_widths": table.ColumnWidths})

print(json.dumps(result, indent=2))

Sử dụng hàng đầu tiên làm tiêu đề

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

doc = Document("MyNotes.one")

for table in doc.GetChildNodes(Table):
    rows = table.GetChildNodes(TableRow)
    if not rows:
        continue

    def row_text(row):
        return [
            " ".join(rt.Text for rt in cell.GetChildNodes(RichText)).strip()
            for cell in row.GetChildNodes(TableCell)
        ]

    headers = row_text(rows[0])
    print("Headers:", headers)
    for row in rows[1:]:
        record = dict(zip(headers, row_text(row)))
        print("  Record:", record)

Những gì Thư viện hỗ trợ cho các bảng

Tính năngĐược hỗ trợ
Table.ColumnWidthsCó: độ rộng cột tính bằng điểm
Table.BordersVisible
Table.TagsCó: thẻ OneNote trên các bảng
Văn bản ô qua RichText
Hình ảnh ô qua Image
Các ô đã hợp nhất (siêu dữ liệu rowspan/colspan)Không được công khai trong API công cộng
Viết/chỉnh sửa bảng và lưu vào .oneKhông

Các bước tiếp theo