מדריך למפתחים

Aspose.Note FOSS for Python is a free, open-source library for reading Microsoft OneNote .one קבצי מדור ללא תלות במיקרוסופט אופיס. הוא מספק API ציבורי נקי תחת ה aspose.note package, מודל על פי Aspose.Note עבור ממשק .NET. הספרייה מתאימה לאוטומציה של מסמכים, אינדקסינג תוכן, צינורות חילוץ נתונים, וזרימות עבודה של ארכיון.

מדריך מפתחים זה מכסה את כל המשטח של ה-API הציבורי הזמין בגרסה 26.3.1, עם דוגמאות קוד ניתנות להרצה עבור כל תכונה מרכזית.

טעינת מסמך

טען .one קובץ מנתיב קובץ או מזרם בינארי. ה Document המחלקה היא נקודת הכניסה לכל הפעולות.

טעינה ממסלול קובץ

from aspose.note import Document

doc = Document("MyNotes.one")

טעינה מזרם בינארי

שימושי כאשר קוראים מאחסון ענן, תגובות HTTP, או חוצצים בזיכרון:

from pathlib import Path
from aspose.note import Document

with Path("MyNotes.one").open("rb") as f:
    doc = Document(f)

אפשרויות טעינה

השתמש LoadOptions כדי להגדיר פרמטרים אופציונליים בזמן הטעינה:

from aspose.note import Document, LoadOptions

opts = LoadOptions()
opts.LoadHistory = True   # Include page history in the DOM
doc = Document("MyNotes.one", opts)

הערה: DocumentPassword קיים ב LoadOptions לתאימות API, אך מסמכים מוצפנים אינם נתמכים. ניסיון לטעון קובץ מוצפן גורם ל IncorrectPasswordException.


מבנה המסמך (DOM)

מודל המסמך של OneNote הוא עץ:

Document
  └── Page (0..n)
        ├── Title
        │     ├── TitleText (RichText)
        │     ├── TitleDate (RichText)
        │     └── TitleTime (RichText)
        └── Outline (0..n)
              └── OutlineElement (0..n)
                    ├── RichText
                    ├── Image
                    ├── Table
                    │     └── TableRow
                    │           └── TableCell
                    │                 └── RichText / Image
                    └── AttachedFile

כל צומת חושף ParentNode ו Document מאפיין שמטפס עד לשורש. צמתים מורכבים תומכים באיטרציה של ילדים, FirstChild, LastChild, AppendChildLast, InsertChild, RemoveChild, ו GetChildNodes(Type).


איטרציה על דפים

דפים הם הילדים הישירים של Document. איטרציה עליהם ישירות או השתמש GetChildNodes:

from aspose.note import Document, Page

doc = Document("MyNotes.one")

for page in doc:
    title = page.Title.TitleText.Text if page.Title and page.Title.TitleText else "(untitled)"
    author = page.Author or "(unknown)"
    print(f"  {title}  [by {author}]")

מטא-נתוני דף:

מאפייןסוגתיאור
Title`TitleNone`
Author`strNone`
CreationTime`datetimeNone`
LastModifiedTime`datetimeNone`
Level`intNone`

חילוץ טקסט

צאץ את כל הטקסט הפשוט

from aspose.note import Document, RichText

doc = Document("MyNotes.one")
all_text = [rt.Text for rt in doc.GetChildNodes(RichText) if rt.Text]
print("\n".join(all_text))

בדוק רצפי עיצוב

כל RichText מכיל רשימה של TextRun קטעים. כל ריצה נושאת את שלה TextStyle:

from aspose.note import Document, RichText

doc = Document("FormattedNotes.one")
for rt in doc.GetChildNodes(RichText):
    for run in rt.TextRuns:
        style = run.Style
        flags = []
        if style.IsBold: flags.append("bold")
        if style.IsItalic: flags.append("italic")
        if style.IsHyperlink: flags.append(f"link={style.HyperlinkAddress}")
        print(f"{run.Text!r:40s} [{', '.join(flags)}]")

חילץ קישורים

from aspose.note import Document, RichText

doc = Document("MyNotes.one")
for rt in doc.GetChildNodes(RichText):
    for run in rt.TextRuns:
        if run.Style.IsHyperlink and run.Style.HyperlinkAddress:
            print(run.Text, "->", run.Style.HyperlinkAddress)

חילוץ תמונות

from aspose.note import Document, Image

doc = Document("MyNotes.one")
for i, img in enumerate(doc.GetChildNodes(Image), start=1):
    name = img.FileName or f"image_{i}.bin"
    with open(name, "wb") as f:
        f.write(img.Bytes)
    print(f"Saved {name}  ({img.Width}x{img.Height})")

מאפייני תמונה: FileName, Bytes, Width, Height, AlternativeTextTitle, AlternativeTextDescription, HyperlinkUrl, Tags.


פענוח טבלאות

from aspose.note import Document, Table, TableRow, TableCell, RichText

doc = Document("MyNotes.one")
for table in doc.GetChildNodes(Table):
    print("Column widths:", [col.Width for col in table.Columns])
    for r, row in enumerate(table.GetChildNodes(TableRow), start=1):
        cells = row.GetChildNodes(TableCell)
        row_text = [
            " ".join(rt.Text for rt in cell.GetChildNodes(RichText)).strip()
            for cell in cells
        ]
        print(f"Row {r}:", row_text)

קבצים מצורפים

from aspose.note import Document, AttachedFile

doc = Document("NotesWithAttachments.one")
for i, af in enumerate(doc.GetChildNodes(AttachedFile), start=1):
    name = af.FileName or f"attachment_{i}.bin"
    with open(name, "wb") as f:
        f.write(af.Bytes)
    print(f"Saved: {name}")

תגיות ורשימות ממוספרות

בדוק פריטי NoteTag

from aspose.note import Document, RichText, Image, Table

doc = Document("TaggedNotes.one")
for rt in doc.GetChildNodes(RichText):
    for tag in rt.Tags:
        print(f"RichText tag: {tag.Label} icon={tag.Icon}")
for img in doc.GetChildNodes(Image):
    for tag in img.Tags:
        print(f"Image tag: {tag.Label}")

בדוק רשימות ממוספרות

from aspose.note import Document, OutlineElement

doc = Document("NumberedNotes.one")
for oe in doc.GetChildNodes(OutlineElement):
    nl = oe.NumberList
    if nl:
        print(f"format={nl.Format!r}")

תבנית DocumentVisitor

השתמש DocumentVisitor כדי לממש מבקר שמטייל בעץ המסמך כולו:

from aspose.note import Document, DocumentVisitor, Page, RichText, Image

class ContentCounter(DocumentVisitor):
    def __init__(self):
        self.pages = 0
        self.texts = 0
        self.images = 0

    def VisitPageStart(self, page: Page) -> None:
        self.pages += 1

    def VisitRichTextStart(self, rt: RichText) -> None:
        self.texts += 1

    def VisitImageStart(self, img: Image) -> None:
        self.images += 1

doc = Document("MyNotes.one")
counter = ContentCounter()
doc.Accept(counter)
print(f"Pages: {counter.pages}, Texts: {counter.texts}, Images: {counter.images}")

ייצוא PDF

ייצוא PDF דורש את התלות האופציונלית ReportLab. התקן זאת באמצעות:

pip install "aspose-note[pdf]"

ייצוא PDF בסיסי

from aspose.note import Document, SaveFormat

doc = Document("MyNotes.one")
doc.Save("output.pdf", SaveFormat.Pdf)

ייצוא PDF עם אפשרויות

import io
from aspose.note import Document, SaveFormat
from aspose.note.saving import PdfSaveOptions

doc = Document("MyNotes.one")

##With save options
opts = PdfSaveOptions()
doc.Save("output.pdf", opts)

##Save to in-memory stream
buf = io.BytesIO()
doc.Save(buf, PdfSaveOptions())
pdf_bytes = buf.getvalue()

הערה: PdfSaveOptions.PageIndex ו PageCount שדות קיימים אך אינם מועברים למייצא ה‑PDF ב‑v26.3.1. המסמך כולו תמיד מיוצא.


מגבלות נוכחיות

אזורסטטוס
קריאה .one קבציםנתמך במלואו
ייצוא PDF (דרך ReportLab)נתמך
כתיבה חזרה ל .oneלא מיושם
מסמכים מוצפניםלא נתמך (מעלה IncorrectPasswordException)
פורמטים לשמירה של HTML / תמונה / ONEמוצהר לתאימות API; מעלה UnsupportedSaveFormatException

מדריכים זמינים

  • סקירת תכונות: רשימת תכונות מלאה עם ראיות
  • התחלה: דרישות מוקדמות, התקנה, והצעדים הראשונים
  • התקנה: pip install ותלויות אופציונליות

ראה גם

 עברית