استخراج النص من ملفات OneNote باستخدام Python
إذا كنت بحاجة إلى قراءة النص من Microsoft OneNote .one الملفات في سيناريو Python، دون تثبيت Microsoft Office أو تشغيل Windows،, Aspose.Note FOSS for Python إنها مكتبة مفتوحة المصدر 100٪ التي تنتشر في شكل OneNote الثنائي مباشرة وتعرض API Python النظيف.
تثبيت
pip install aspose-noteلا مفتاح API. لا ملف ترخيص. ليس Microsoft Office.
أسهل نهج: GetChildNodes(RichText)
يتم تخزين النص OneNote في RichText العقد الموزعة عبر الصفحات، والخطوط الخلفية، وعناصر الخطوط. GetChildNodes(RichText) يمارس البحث المتكرر عن شجرة المستند بأكملها ويعيد كل عقدة نصية كقائمة مسطحة:
from aspose.note import Document, RichText
doc = Document("MyNotes.one")
for rt in doc.GetChildNodes(RichText):
if rt.Text:
print(rt.Text)
هذه هي أسرع طريقة للحصول على كل محتوى النص من .one ملف .
حفظ النص إلى ملف
from aspose.note import Document, RichText
doc = Document("MyNotes.one")
lines = [rt.Text for rt in doc.GetChildNodes(RichText) if rt.Text]
with open("extracted.txt", "w", encoding="utf-8") as f:
f.write("\n".join(lines))
print(f"Saved {len(lines)} text blocks to extracted.txt")
نص النص لكل صفحة
عندما تحتاج إلى معرفة الصفحة التي جاءت من كل كتلة نصية:
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)"
)
page_texts = [rt.Text for rt in page.GetChildNodes(RichText) if rt.Text]
print(f"\n=== {title} ===")
for text in page_texts:
print(text)
استخراج hyperlinks
يتم تخزين الروابط الفردية على TextRun الموضوعات داخل RichText الوسوم - التحقق run.Style.IsHyperlink:
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"{run.Text!r} -> {run.Style.HyperlinkAddress}")
تنسيق الكشف: Bold, إيطالي, Underline
كل واحد منهم TextRun يقدم شخصية من خلالها TextStyle:
from aspose.note import Document, RichText
doc = Document("MyNotes.one")
for rt in doc.GetChildNodes(RichText):
for run in rt.TextRuns:
s = run.Style
if any([s.IsBold, s.IsItalic, s.IsUnderline]):
flags = ", ".join(f for f, v in [
("bold", s.IsBold), ("italic", s.IsItalic), ("underline", s.IsUnderline)
] if v)
print(f"[{flags}] {run.Text.strip()!r}")
قراءة من سلك
يعمل مع التخزين السحابي، و HTTP ردود الفعل الهيئات، أو في الذاكرة المضغوطات:
import io, urllib.request
from aspose.note import Document, RichText
##Example: load from bytes already in memory
one_bytes = open("MyNotes.one", "rb").read()
doc = Document(io.BytesIO(one_bytes))
texts = [rt.Text for rt in doc.GetChildNodes(RichText) if rt.Text]
print(f"Extracted {len(texts)} text block(s)")
Windows إصلاح التشفير
في محطات ويندوز،, sys.stdout قد تستخدم رمز التراث الذي يخترق على أحرف Unicode.إضافة هذا في بداية السيناريو الخاص بك:
import sys
if hasattr(sys.stdout, "reconfigure"):
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
ما يدعم المكتبة
| الميزات | دعم |
|---|---|
قراءة .one الملفات (الطريق أو التدفق) | نعم |
استخراج RichText.Text (النص المباشر ) | نعم |
المفتشين TextRun.Style (باللغة الإيطالية، الارتباط، الخط) | نعم |
| استخراج النص من الخلايا المكتبية | نعم |
| اقرأ صفحة العناوين | نعم |
اكتب مرة أخرى إلى .one | لا |
| وثائق مشفرة | لا |