如何使用 Python 解析 OneNote 文件中的表格

如何使用 Python 解析 OneNote 文件中的表格

Microsoft OneNote 允许用户直接在页面中嵌入结构化数据表格。Aspose.Note FOSS for Python 通过 Table → TableRow → TableCell 层次结构公开每个表格,使您能够以编程方式访问所有单元格内容、列元数据和表格标签。

优势

  1. 结构化访问:行和列计数、单元格内容、列宽度
  2. 无需电子表格应用:从任何平台的 OneNote 中提取表格数据
  3. 免费且开源:MIT 许可证,无需 API 密钥

分步指南

步骤 1:安装 Aspose.Note FOSS for Python

pip install aspose-note

步骤 2:加载 .one 文件

from aspose.note import Document

doc = Document("MyNotes.one")
print(f"Pages: {len(list(doc))}")

步骤 3:查找所有表格

使用 GetChildNodes(Table) 递归检索整个文档中的所有表格:

from aspose.note import Document, Table

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

步骤 4:读取行和单元格值

遍历 TableRowTableCell 节点。每个单元格包含 RichText 节点,其 .Text 属性提供纯文本内容:

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}")

步骤 5:读取列宽

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}")

步骤 6: 导出为 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")

常见问题及解决方案

表格显示为空

原因: 单元格包含 Image 节点,而不是 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 = cell.GetChildNodes(RichText)
            images = cell.GetChildNodes(Image)
            print(f"  Cell: {len(texts)} text(s), {len(images)} image(s)")

列数不匹配 Columns

table.Columns 反映文件中存储的列元数据。如果行中有合并单元格,实际每行的单元格数量可能不同(文件格式在二进制层面存储此信息;公共 API 不会暴露合并标志)。

ImportError: 没有名为 ‘aspose’ 的模块

pip install aspose-note
pip show aspose-note  # confirm it is installed in the active environment

常见问题

我可以编辑表格数据并保存回去吗? 不支持将数据写回 .one 格式。内存中所做的更改(例如通过 RichText.Replace())无法持久化到源文件。

是否检测到合并单元格? CompositeNode API 不会公开合并元数据。每个 TableCell 都被视为独立的单元格,无论视觉上是否合并。

我可以统计表格有多少行吗? 是的:len(table.GetChildNodes(TableRow))


相关资源:

 中文