Python을 사용하여 OneNote 파일에서 표 파싱하는 방법

Python을 사용하여 OneNote 파일에서 표 파싱하는 방법

Microsoft OneNote는 사용자가 페이지에 구조화된 데이터 테이블을 직접 삽입할 수 있도록 합니다. Aspose.Note FOSS for Python은 Table → TableRow → TableCell 계층 구조를 통해 모든 테이블을 노출하여 셀 내용, 열 메타데이터 및 테이블 태그에 대한 프로그래밍 방식 접근을 제공합니다.

혜택

  1. 구조화된 접근: 행 및 열 개수, 개별 셀 내용, 열 너비
  2. 스프레드시트 앱 불필요: 모든 플랫폼에서 OneNote의 표 데이터 추출
  3. 무료 및 오픈소스: MIT 라이선스, API 키 없음

단계별 가이드

1단계: Python용 Aspose.Note FOSS 설치

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)).


관련 리소스:

 한국어