Python에서 OneNote 테이블 파싱
Microsoft OneNote는 사용자가 페이지에 구조화된 테이블을 직접 삽입할 수 있게 하며, 작업 목록, 일정, 비교 매트릭스, 데이터 수집 양식 등에 적합합니다. Aspose.Note FOSS for Python는 Microsoft Office를 설치하지 않고도 프로그래밍 방식으로 이러한 모든 표 데이터를 추출할 수 있게 합니다.
설치
pip install aspose-note
문서를 로드하고 테이블 찾기
GetChildNodes(Table) 전체 문서를 재귀적으로 검색하고 모든 표를 객체로 반환합니다: Table 객체:
from aspose.note import Document, Table
doc = Document("MyNotes.one")
tables = doc.GetChildNodes(Table)
print(f"Found {len(tables)} table(s)")
셀 값 읽기
표는 3단계 계층 구조를 따릅니다: Table → TableRow → TableCell. 각 셀은 포함합니다 RichText 노드 중 .Text 평문 텍스트 내용을 제공합니다:
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}")
열 너비 검사
Table.ColumnWidths 각 열의 저장된 너비를 포인트 단위로 반환합니다:
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}")
모든 테이블을 CSV로 내보내기
문서의 모든 테이블을 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")
테이블을 Python 딕셔너리 / JSON으로 내보내기
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))
첫 번째 행을 헤더로 사용
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)
라이브러리가 테이블에 대해 지원하는 기능
| 기능 | 지원됨 |
|---|---|
Table.ColumnWidths | 예: 포인트 단위 열 너비 |
Table.BordersVisible | 예 |
Table.Tags | 예: 표에 대한 OneNote 태그 |
셀 텍스트 via RichText | 예 |
셀 이미지 via Image | 예 |
| 병합된 셀 (rowspan/colspan 메타데이터) | 공개 API에 노출되지 않음 |
표를 작성/편집하고 저장하기 .one | 아니오 |