表格解析 — Aspose.Note FOSS for Python
OneNote 文档中的表格以三级层次结构呈现:: Table → TableRow → TableCell. 每个单元格可以包含 RichText, Image,,以及其他内容节点。本页涵盖了 Aspose.Note FOSS 对 Python 支持的所有表格解析模式。.
基本表格遍历
检索文档中的所有表格并读取其单元格文本::
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}")表格属性
| 属性 | 类型 | 描述 |
|---|---|---|
Columns | list[TableColumn] | 列定义;每个 TableColumn 具有 .Width (float) 和 .LockedWidth (bool) |
IsBordersVisible | bool | 表格边框是否显示 |
Tags | list[NoteTag] | 附加在表格上的 OneNote 标签 |
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}")导出表格为 CSV
将表格转换为 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())按页提取表格
将表格提取范围限定为单独的页面::
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}")单元格内容超出纯文本
表格单元格可以包含 Image 以及其他 CompositeNode 内容以及 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)}")统计并汇总表格
收集文档中所有表格的统计信息::
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]}")检查表格上的标签
表格支持 NoteTag 直接项目::
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}")表格的 DOM 位置
表格显示为以下的子节点 OutlineElement 内部的节点 Outline 每个容器 Page. 层次结构是::
Page
└── Outline
└── OutlineElement
└── Table
└── TableRow
└── TableCell
└── RichText / Image您也可以通过以下方式访问表格 GetChildNodes(Table) 在任何祖先节点层级,且它会搜索完整子树。.
提示
table.Columns是一个列表TableColumn对象;使用[col.Width for col in table.Columns]以获取列宽(单位为点)。长度等于列数。.- 单元格内容并不总是纯粹的
RichText;;始终检查Image节点,如果完整保真度很重要。. - 使用
table.GetChildNodes(TableRow)而不是迭代for row in table如果您需要一个强类型列表而不是通用迭代器。. IsBordersVisible反映了 OneNote 用户在保存时的显示偏好;它不影响内容提取。.