Cách Phân Tích Bảng trong Tệp OneNote bằng Python
Microsoft OneNote cho phép người dùng nhúng các bảng dữ liệu có cấu trúc trực tiếp trong các trang. Aspose.Note FOSS for Python cung cấp mọi bảng thông qua một cấu trúc Table → TableRow → TableCell, cho phép bạn truy cập lập trình vào tất cả nội dung ô, siêu dữ liệu cột và thẻ bảng.
Lợi ích
- Truy cập có cấu trúc: số lượng hàng và cột, nội dung ô riêng lẻ, độ rộng cột
- Không cần ứng dụng bảng tính: trích xuất dữ liệu bảng từ OneNote trên bất kỳ nền tảng nào
- Miễn phí và mã nguồn mở: giấy phép MIT, không cần khóa API
Hướng Dẫn Từng Bước
Bước 1: Cài đặt Aspose.Note FOSS cho Python
pip install aspose-noteBước 2: Tải tệp .one
from aspose.note import Document
doc = Document("MyNotes.one")
print(f"Pages: {len(list(doc))}")Bước 3: Tìm tất cả các bảng
Sử dụng GetChildNodes(Table) để truy xuất mọi bảng từ toàn bộ tài liệu một cách đệ quy:
from aspose.note import Document, Table
doc = Document("MyNotes.one")
tables = doc.GetChildNodes(Table)
print(f"Found {len(tables)} table(s)")Bước 4: Đọc giá trị hàng và ô
Lặp lại các nút TableRow và TableCell. Mỗi ô chứa các nút RichText mà thuộc tính .Text của chúng cung cấp nội dung dạng văn bản thuần:
from aspose.note import Document, Table, TableRow, TableCell, RichText
doc = Document("MyNotes.one")
for t, table in enumerate(doc.GetChildNodes(Table), start=1):
print(f"\nTable {t}: {len(table.Columns)} column(s)")
for r, row in enumerate(table.GetChildNodes(TableRow), start=1):
cell_values = []
for cell in row.GetChildNodes(TableCell):
text = " ".join(rt.Text for rt in cell.GetChildNodes(RichText)).strip()
cell_values.append(text)
print(f" Row {r}: {cell_values}")Bước 5: Đọc độ rộng cột
from aspose.note import Document, Table
doc = Document("MyNotes.one")
for i, table in enumerate(doc.GetChildNodes(Table), start=1):
print(f"Table {i} column widths (pts): {[col.Width for col in table.Columns]}")
print(f"Borders visible: {table.IsBordersVisible}")Bước 6: Xuất sang CSV
import csv, io
from aspose.note import Document, Table, TableRow, TableCell, RichText
doc = Document("MyNotes.one")
buf = io.StringIO()
writer = csv.writer(buf)
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(buf.getvalue())
print("Saved tables.csv")Các vấn đề thường gặp và cách khắc phục
Bảng hiển thị trống
Nguyên nhân: Các tế bào chứa các nút Image thay vì các nút RichText.
Kiểm tra:
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 = cell.GetChildNodes(RichText)
images = cell.GetChildNodes(Image)
print(f" Cell: {len(texts)} text(s), {len(images)} image(s)")Số cột không khớp Columns
table.Columns phản ánh siêu dữ liệu cột được lưu trong tệp. Số lượng ô thực tế trên mỗi hàng có thể khác nhau nếu các hàng có ô được hợp nhất (định dạng tệp lưu trữ điều này ở mức nhị phân; API công cộng không tiết lộ cờ hợp nhất).
ImportError: Không có mô-đun nào có tên ‘aspose’
pip install aspose-note
pip show aspose-note # confirm it is installed in the active environmentCâu hỏi thường gặp
Tôi có thể chỉnh sửa dữ liệu bảng và lưu lại không? Không. Việc ghi lại vào định dạng .one không được hỗ trợ. Các thay đổi được thực hiện trong bộ nhớ (ví dụ: qua RichText.Replace()) không thể được lưu lại vào tệp nguồn.
Các ô đã hợp nhất có được phát hiện không? API CompositeNode không cung cấp siêu dữ liệu hợp nhất. Mỗi TableCell được coi là một ô riêng biệt bất kể việc hợp nhất trực quan.
Có thể đếm bao nhiêu hàng trong một bảng không? Có: len(table.GetChildNodes(TableRow)).
Tài nguyên liên quan: