تنظيف مستند
في بعض الأحيان قد تحتاج إلى إزالة المعلومات غير المستخدمة أو المكررة لتقليل حجم مستند الإخراج ووقت المعالجة.
على الرغم من أنه يمكنك العثور على البيانات غير المستخدمة وإزالتها، مثل الأنماط أو القوائم، أو المعلومات المكررة يدويًا، إلا أنه سيكون أكثر ملاءمة للقيام بذلك باستخدام الميزات والإمكانيات التي يوفرها Aspose.Words.
تتيح لك فئة CleanupOptions تحديد خيارات لتنظيف المستندات. لإزالة الأنماط المكررة أو الأنماط أو القوائم غير المستخدمة من المستند، يمكنك استخدام طريقة cleanup.
إزالة المعلومات غير المستخدمة من مستند
يمكنك استخدام خصائص unused_styles وunused_builtin_styles للكشف عن الأنماط التي تم وضع علامة “غير مستخدمة” عليها وإزالتها.
يمكنك استخدام خاصية unused_lists للكشف عن القوائم وتعريفات القوائم التي تم وضع علامة “غير مستخدمة” عليها وإزالتها.
يوضح مثال التعليمات البرمجية التالي كيفية إزالة الأنماط غير المستخدمة فقط من المستند:
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET | |
doc = aw.Document(docs_base.my_dir + "Unused styles.docx") | |
# Combined with the built-in styles, the document now has eight styles. | |
# A custom style is marked as "used" while there is any text within the document | |
# formatted in that style. This means that the 4 styles we added are currently unused. | |
print(f"Count of styles before Cleanup: {doc.styles.count}\n" + | |
f"Count of lists before Cleanup: {doc.lists.count}") | |
# Cleans unused styles and lists from the document depending on given CleanupOptions. | |
cleanupOptions = aw.CleanupOptions() | |
cleanupOptions.unused_lists = False | |
cleanupOptions.unused_styles = True | |
doc.cleanup(cleanupOptions) | |
print(f"Count of styles after Cleanup was decreased: {doc.styles.count}\n" + | |
f"Count of lists after Cleanup is the same: {doc.lists.count}") | |
doc.save(docs_base.artifacts_dir + "WorkingWithDocumentOptionsAndSettings.cleanup_unused_styles_and_lists.docx") |
إزالة المعلومات المكررة من مستند
يمكنك أيضًا استخدام خاصية duplicate_style لاستبدال جميع الأنماط المكررة بالنمط الأصلي وإزالة التكرارات من المستند.
يوضح مثال التعليمات البرمجية التالي كيفية إزالة الأنماط المكررة من مستند:
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET | |
doc = aw.Document(docs_base.my_dir + "Document.docx") | |
# Count of styles before Cleanup. | |
print(doc.styles.count) | |
# Cleans duplicate styles from the document. | |
options = aw.CleanupOptions() | |
options.duplicate_style = True | |
doc.cleanup(options) | |
# Count of styles after Cleanup was decreased. | |
print(doc.styles.count) | |
doc.save(docs_base.artifacts_dir + "WorkingWithDocumentOptionsAndSettings.cleanup_duplicate_style.docx") |