使用 Word 文档的选项和外观
有时您可能需要更改文档的外观,例如,设置语言首选项或每页行数。Aspose.Words 提供了控制文档显示方式的功能,以及一些附加选项。本文描述了这种可能性。
设置文档显示选项
您可以使用 ViewOptions 类控制文档在 Microsoft Word 中的显示方式。例如,您可以使用 zoom_percent 属性设置文档缩放值,或使用 view_type 属性设置查看模式。
以下代码示例演示如何确保文档在 Microsoft Word 中打开时显示为 50%:
# 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") | |
doc.view_options.view_type = aw.settings.ViewType.PAGE_LAYOUT | |
doc.view_options.zoom_percent = 50 | |
doc.save(docs_base.artifacts_dir + "WorkingWithDocumentOptionsAndSettings.view_options.docx") |
设置页面显示选项
如果要设置每行的字符数,请使用 characters_per_line 属性。您还可以设置 Word 文档每页的行数 - 使用 lines_per_page 属性获取或设置文档网格中每页的行数。
以下代码示例显示如何设置 Microsoft Word 文档的每行字符数和每页行数:
# 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") | |
# Set the layout mode for a section allowing to define the document grid behavior. | |
# Note that the Document Grid tab becomes visible in the Page Setup dialog of MS Word | |
# if any Asian language is defined as editing language. | |
doc.first_section.page_setup.layout_mode = aw.SectionLayoutMode.GRID | |
doc.first_section.page_setup.characters_per_line = 30 | |
doc.first_section.page_setup.lines_per_page = 10 | |
doc.save(docs_base.artifacts_dir + "WorkingWithDocumentOptionsAndSettings.document_page_setup.docx") |
设置语言首选项
以 Microsoft Word 格式显示文档取决于该文档的默认语言设置。如果默认情况下未设置语言,Microsoft Word 会从"设置 Office 语言首选项"对话框中获取信息,例如,可以在 Microsoft Word 2019 中的"文件 → 选项 → 语言"下找到该信息。
通过 Aspose.Words,您还可以使用 LanguagePreferences 类设置语言首选项。另请注意,为了正确显示文档,有必要设置文档加载过程应匹配的 Microsoft Word 版本 - 这可以使用 msw_version 属性来完成。
以下代码示例展示了如何将日语添加到编辑语言中:
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET | |
loadOptions = aw.loading.LoadOptions() | |
# Set language preferences that will be used when document is loading. | |
loadOptions.language_preferences.add_editing_language(aw.loading.EditingLanguage.JAPANESE) |
以下代码示例显示如何将俄语设置为默认编辑语言:
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET | |
loadOptions = aw.loading.LoadOptions() | |
loadOptions.language_preferences.default_editing_language = aw.loading.EditingLanguage.RUSSIAN | |
doc = aw.Document(docs_base.my_dir + "No default editing language.docx", loadOptions) | |
localeId = doc.styles.default_font.locale_id | |
print("The document either has no any language set in defaults or it was set to Russian originally." if (localeId == aw.loading.EditingLanguage.RUSSIAN) | |
else "The document default language was set to another than Russian language originally, so it is not overridden.") |
针对特定 Word 版本优化文档
optimize_for 方法允许优化文档内容以及特定版本 Microsoft Word 的默认 Aspose.Words 行为。您可以使用此方法来防止 Microsoft Word 在文档加载时显示"兼容模式"功能区。请注意,您可能还需要将 compliance 属性设置为 ISO29500_2008_TRANSITIONAL 或更高版本。
以下代码示例展示了如何优化 Microsoft Word 2016 的文档内容:
# 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") | |
doc.compatibility_options.optimize_for(aw.settings.MsWordVersion.WORD2016) | |
doc.save(docs_base.artifacts_dir + "WorkingWithDocumentOptionsAndSettings.optimize_for_ms_word.docx") |