วิธีแยกตารางในไฟล์ OneNote ด้วย Python
Microsoft OneNote ให้ผู้ใช้ฝังตารางข้อมูลที่มีโครงสร้างโดยตรงในหน้า Aspose.Note FOSS for Python เปิดเผยทุกตารางผ่านลำดับชั้น Table → TableRow → TableCell ให้คุณเข้าถึงเนื้อหาเซลล์ทั้งหมด เมตาดาต้าคอลัมน์ และแท็กของตารางแบบโปรแกรมเมติก.
ประโยชน์
- การเข้าถึงแบบมีโครงสร้าง: จำนวนแถวและคอลัมน์, เนื้อหาเซลล์แต่ละเซลล์, ความกว้างของคอลัมน์
- ไม่จำเป็นต้องใช้แอปสเปรดชีต: ดึงข้อมูลตารางจาก OneNote บนแพลตฟอร์มใดก็ได้
- ฟรีและโอเพ่นซอร์ส: ใบอนุญาต 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 nodes. แต่ละ cell มี RichText nodes ที่ .Text property ให้เนื้อหา plain-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 สะท้อนเมตาดาต้าคอลัมน์ที่จัดเก็บในไฟล์ จำนวนเซลล์ต่อแถวจริงอาจแตกต่างกันหากแถวมีการรวมเซลล์ (รูปแบบไฟล์จัดเก็บข้อมูลนี้ในระดับไบนารี; public API ไม่ได้เปิดเผย merge flag)
ImportError: ไม่พบโมดูลชื่อ ‘aspose’
pip install aspose-note
pip show aspose-note # confirm it is installed in the active environmentคำถามที่พบบ่อย
ฉันสามารถแก้ไขข้อมูลตารางและบันทึกกลับได้หรือไม่? ไม่. การเขียนกลับไปยังรูปแบบ .one ไม่ได้รับการสนับสนุน. การเปลี่ยนแปลงที่ทำในหน่วยความจำ (เช่น ผ่าน RichText.Replace()) ไม่สามารถบันทึกลงไฟล์ต้นฉบับได้.
ตรวจพบเซลล์ที่รวมกันหรือไม่? API CompositeNode ไม่เปิดเผยเมตาดาต้าการรวมเซลล์ แต่ละ TableCell จะถูกพิจารณาเป็นเซลล์แยกกันโดยไม่คำนึงถึงการรวมที่มองเห็นได้
ฉันสามารถนับจำนวนแถวในตารางได้หรือไม่? ใช่: len(table.GetChildNodes(TableRow)).
แหล่งข้อมูลที่เกี่ยวข้อง: