كيفية استخراج النص من ملفات OneNote باستخدام Python

كيفية استخراج النص من ملفات OneNote باستخدام Python

ملفات Microsoft OneNote .one هي مستندات ثنائية لا يمكن قراءتها كنص عادي أو تحليلها بأدوات XML العامة. يوفر Aspose.Note FOSS for Python محللًا نقيًا بلغة Python يقوم بتحميل ملفات .one إلى نموذج كائن مستند كامل (DOM)، مما يجعل استخراج النص، وبيانات التنسيق الوصفية، والروابط التشعبية برمجيًا أمرًا بسيطًا.

فوائد استخدام Aspose.Note FOSS للغة بايثون

  1. لا حاجة إلى Microsoft Office: قراءة ملفات .one على أي منصة، بما في ذلك خوادم Linux CI/CD
  2. الوصول الكامل إلى النص والتنسيق: نص عادي، تشغيلات غامقة/مائلة/تحته خط، خصائص الخط، وعناوين URL للروابط التشعبية
  3. مجاني ومفتوح المصدر: ترخيص MIT، لا رسوم استخدام ولا مفاتيح API

دليل خطوة بخطوة

الخطوة 1: تثبيت Aspose.Note FOSS لـ Python

قم بتثبيت المكتبة من PyPI. لا تحتوي الحزمة الأساسية على أي تبعيات إلزامية:

pip install aspose-note

تحقق من التثبيت:

from aspose.note import Document
print("Installation OK")

الخطوة 2: تحميل ملف .one

إنشاء مثيل Document بتمرير مسار الملف:

from aspose.note import Document

doc = Document("MyNotes.one")
print(f"Section: {doc.DisplayName}")
print(f"Pages:   {len(list(doc))}")

لتحميل من تدفق ثنائي (مثلاً من التخزين السحابي أو استجابة HTTP):

from aspose.note import Document

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

الخطوة 3: استخراج كل النص العادي

استخدم GetChildNodes(RichText) لجمع كل عقدة RichText في شجرة المستند. يقوم هذا بإجراء بحث عميق أولاً بطريقة تكرارية عبر جميع الصفحات والملخصات وعناصر الملخص:

from aspose.note import Document, RichText

doc = Document("MyNotes.one")
texts = [rt.Text for rt in doc.GetChildNodes(RichText) if rt.Text]

for text in texts:
    print(text)

لحفظ كل النص في ملف:

from aspose.note import Document, RichText

doc = Document("MyNotes.one")
texts = [rt.Text for rt in doc.GetChildNodes(RichText) if rt.Text]

with open("extracted_text.txt", "w", encoding="utf-8") as out:
    out.write("\n".join(texts))

print(f"Wrote {len(texts)} text blocks to extracted_text.txt")

الخطوة 4: فحص المقاطع المنسقة

كل عقدة RichText تحتوي على قائمة TextRuns من مقاطع TextRun. كل تشغيل يحمل TextStyle مستقلاً مع تنسيق لكل حرف:

from aspose.note import Document, RichText

doc = Document("MyNotes.one")

for rt in doc.GetChildNodes(RichText):
    for run in rt.TextRuns:
        style = run.Style
        attrs = []
        if style.IsBold:        attrs.append("bold")
        if style.IsItalic:      attrs.append("italic")
        if style.IsUnderline:   attrs.append("underline")
        if style.IsStrikethrough: attrs.append("strikethrough")
        if style.FontName:    attrs.append(f"font={style.FontName}")
        if style.FontSize:    attrs.append(f"size={style.FontSize}pt")
        label = ", ".join(attrs) if attrs else "plain"
        print(f"[{label}] {run.Text!r}")

الخطوة 5: استخراج الروابط التشعبية

يتم تخزين الروابط التشعبية على عقد TextRun الفردية. تحقق من Style.IsHyperlink واقرأ Style.HyperlinkAddress:

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(f"Link text: {run.Text!r}")
            print(f"URL:       {run.Style.HyperlinkAddress}")

الخطوة 6: استخراج النص لكل صفحة

لاستخراج النص منظمًا حسب عنوان الصفحة:

from aspose.note import Document, Page, RichText

doc = Document("MyNotes.one")

for page in doc.GetChildNodes(Page):
    title = (
        page.Title.TitleText.Text
        if page.Title and page.Title.TitleText
        else "(untitled)"
    )
    print(f"\n=== {title} ===")
    for rt in page.GetChildNodes(RichText):
        if rt.Text:
            print(rt.Text)

المشكلات الشائعة والحلول

1. ImportError: لا توجد وحدة باسم ‘aspose’

السبب: الحزمة غير مثبتة في بيئة بايثون النشطة.

إصلاح:

pip install aspose-note
##Confirm active environment:
pip show aspose-note

2. FileNotFoundError عند تحميل ملف .one

السبب: مسار الملف غير صحيح أو الملف غير موجود.

الإصلاح: استخدم مسارًا مطلقًا أو تحقق من وجود الملف قبل التحميل:

from pathlib import Path
from aspose.note import Document

path = Path("MyNotes.one")
if not path.exists():
    raise FileNotFoundError(f"File not found: {path.resolve()}")
doc = Document(str(path))

3. خطأ UnicodeEncodeError على نظام Windows عند الطباعة

السبب: قد تستخدم طرفيات Windows ترميزًا قديمًا لا يمكنه عرض أحرف Unicode.

الإصلاح: أعد تكوين stdout في بداية البرنامج النصي الخاص بك:

import sys
if hasattr(sys.stdout, "reconfigure"):
    sys.stdout.reconfigure(encoding="utf-8", errors="replace")

4. نتائج النص الفارغ

السبب: قد يكون ملف .one فارغًا، أو يحتوي فقط على صور أو جداول (بدون عقد RichText)، أو يكون ملف دفتر ملاحظات (.onetoc2) بدلاً من ملف قسم (.one).

الإصلاح: تحقق من عدد الصفحات وتفقد أنواع العقد:

from aspose.note import Document

doc = Document("MyNotes.one")
print(f"Pages: {len(list(doc))}")
for page in doc:
    print(f"  Children: {sum(1 for _ in page)}")

5. IncorrectPasswordException

السبب: ملف .one مشفر. المستندات المشفرة غير مدعومة.

الإصلاح: Aspose.Note FOSS for Python لا يدعم ملفات .one المشفرة. المنتج التجاري الكامل المميزات Aspose.Note يدعم فك التشفير.


الأسئلة المتكررة

هل يمكنني استخراج النص من جميع الصفحات مرة واحدة؟

نعم. doc.GetChildNodes(RichText) يبحث شجرة المستند بأكملها بشكل متكرر، بما في ذلك جميع الصفحات والملخصات وعناصر الملخص.

هل تدعم المكتبة ملفات دفتر الملاحظات .onetoc2؟

لا. المكتبة تتعامل مع ملفات الأقسام .one فقط. ملفات جدول المحتويات للدفتر (.onetoc2) هي تنسيق مختلف ولا يتم دعمها.

هل يمكنني استخراج النص من الجداول؟

نعم. TableCell العقد تحتوي على RichText أطفال يمكن قراءتهم بنفس الطريقة:

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

doc = Document("MyNotes.one")
for table in doc.GetChildNodes(Table):
    for row in table.GetChildNodes(TableRow):
        for cell in row.GetChildNodes(TableCell):
            cell_text = " ".join(rt.Text for rt in cell.GetChildNodes(RichText)).strip()
            print(cell_text, end="\t")
        print()

ما إصدارات بايثون المدعومة؟

Python 3.10، 3.11، و 3.12.

هل المكتبة آمنة للمعالجة المتعددة الخيوط؟

يجب استخدام كل مثيل Document من خيط واحد. لاستخراج متوازي، أنشئ Document منفصل لكل خيط.


الموارد ذات الصلة:

 العربية