แยกวิเคราะห์ตาราง OneNote ใน Python
Microsoft OneNote ให้ผู้ใช้ฝังตารางที่มีโครงสร้างโดยตรงในหน้า, เหมาะสำหรับรายการงาน, ตารางเวลา, เมทริกซ์เปรียบเทียบ, และแบบฟอร์มการเก็บข้อมูล. Aspose.Note FOSS สำหรับ 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)")
อ่านค่าของเซลล์
ตารางมีโครงสร้างระดับสามชั้น: 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 Dict / 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 บนตาราง |
ข้อความเซลล์ผ่าน RichText | ใช่ |
รูปภาพเซลล์ผ่าน Image | ใช่ |
| เซลล์ที่รวม (เมตาดาต้า rowspan/colspan) | ไม่ได้เปิดเผยใน API สาธารณะ |
เขียน/แก้ไขตารางและบันทึกไปยัง .one | ไม่ |