การแยกตาราง — Aspose.Note FOSS สำหรับ 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 ณ เวลาบันทึก; ไม่ส่งผลต่อการสกัดเนื้อหา.