วิธีแยกวิเคราะห์ตารางในไฟล์ OneNote ด้วย Python

วิธีแยกวิเคราะห์ตารางในไฟล์ OneNote ด้วย Python

Microsoft OneNote ให้ผู้ใช้ฝังตารางข้อมูลที่มีโครงสร้างโดยตรงในหน้า. Aspose.Note FOSS สำหรับ Python เปิดเผยทุกตารางผ่าน a Table → TableRow → TableCell ลำดับชั้น, ให้คุณเข้าถึงข้อมูลเซลล์ทั้งหมด, เมตาดาต้าคอลัมน์, และแท็กของตารางแบบโปรแกรมเมติก.

ประโยชน์

  1. การเข้าถึงแบบมีโครงสร้าง: จำนวนแถวและคอลัมน์, เนื้อหาเซลล์แต่ละอัน, ความกว้างของคอลัมน์
  2. ไม่ต้องใช้แอปสเปรดชีต: ดึงข้อมูลตารางจาก OneNote บนแพลตฟอร์มใดก็ได้
  3. ฟรีและโอเพ่นซอร์ส: ไลเซนส์ MIT, ไม่ต้องใช้คีย์ API

คู่มือแบบขั้นตอนต่อขั้นตอน

ขั้นตอนที่ 1: ติดตั้ง Aspose.Note FOSS สำหรับ 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: อ่านค่าแถวและเซลล์

วนซ้ำ TableRow และ TableCell โหนด. แต่ละเซลล์ประกอบด้วย 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()) ไม่สามารถบันทึกคงไว้ในไฟล์ต้นฉบับได้.

เซลล์ที่รวมกันถูกตรวจจับหรือไม่? The CompositeNode API ไม่เปิดเผยเมตาดาต้าการรวม. แต่ละ TableCell จะถูกจัดเป็นเซลล์แยกโดยไม่คำนึงถึงการรวมแบบมองเห็น.

ฉันสามารถนับจำนวนแถวในตารางได้ไหม? ใช่: len(table.GetChildNodes(TableRow)).


แหล่งข้อมูลที่เกี่ยวข้อง:

 ภาษาไทย