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 vào các trang. Aspose.Note FOSS cho Python tiết lộ mọi bảng thông qua một Table → TableRow → TableCell cây phân cấp, cung cấp cho bạn quyền truy cập lập trình vào toàn bộ 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 TableRow và TableCell các nút. Mỗi ô chứa RichText các nút mà .Text thuộc tính 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 ra 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
Các bảng hiển thị trống
Nguyên nhân: Các ô chứa Image : các nút thay vì RichText : các nút.
: 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ố ô 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 thông tin này ở mức nhị phân; API công cộng không tiết lộ cờ hợp nhất).
ImportError: No module named ‘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. Ghi lại vào .one định dạng không được hỗ trợ. Các thay đổi được thực hiện trong bộ nhớ (ví dụ: thông 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? Cái CompositeNode API 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.
Tôi có thể đếm số hàng của một bảng không? Có: len(table.GetChildNodes(TableRow)).
Tài nguyên liên quan: