Очистити документ
Іноді необхідно видалити невикористану або дублікову інформацію для зменшення розміру вихідного документа і часу обробки.
Хоча ви можете знайти і видалити невикористані дані, такі як стилі або списки, або дублікати інформації вручну, це буде набагато зручніше зробити це за допомогою функцій і можливостей, які надаються за допомогою 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") |