کار با سند متنی
در این مقاله می آموزیم که چه گزینه هایی می توانند برای کار با یک سند متنی از طریق Aspose.Words مفید باشند. لطفاً توجه داشته باشید که این لیست کاملی از گزینه های موجود نیست، بلکه تنها نمونه ای از کار با برخی از آنها است.
علامت های دو جهته را اضافه کنید
میتوانید از ویژگی add_bidi_marks برای تعیین اینکه آیا علامتهای دو جهته قبل از هر اجرا BiDi هنگام صادرات در قالب متن ساده اضافه شود یا خیر استفاده کنید. Aspose.Words نویسه یونیکد “علامت راست به چپ” (U+200F) را قبل از هر Run دو جهته در متن درج می کند. این گزینه مربوط به گزینه “افزودن علائم دو جهته” در گفتگوی تبدیل فایل MS Word هنگام صادرات به یک قالب متن ساده است. توجه داشته باشید که تنها در صورتی در گفتگو ظاهر می شود که یکی از زبان های ویرایش عربی یا عبری در MS Word اضافه شود.
مثال کد زیر نحوه استفاده از ویژگی add_bidi_marks را نشان می دهد. مقدار پیش فرض این ویژگی False
است:
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET | |
doc = aw.Document() | |
builder = aw.DocumentBuilder(doc) | |
builder.writeln("Hello world!") | |
builder.paragraph_format.bidi = True | |
builder.writeln("שלום עולם!") | |
builder.writeln("مرحبا بالعالم!") | |
saveOptions = aw.saving.TxtSaveOptions() | |
saveOptions.add_bidi_marks = True | |
doc.save(docs_base.artifacts_dir + "WorkingWithTxtSaveOptions.add_bidi_marks.txt", saveOptions) |
شناسایی موارد لیست در حین بارگذاری TXT
Aspose.Words میتواند مورد فهرست یک فایل متنی را به عنوان شماره فهرست یا متن ساده در مدل شی سند خود وارد کند. ویژگی detect_numbering_with_whitespaces اجازه می دهد تا مشخص کنید که چگونه موارد لیست شماره گذاری شده هنگام وارد شدن یک سند از قالب متن ساده شناسایی شوند:
- اگر این گزینه روی
True
تنظیم شده باشد، از فضاهای خالی به عنوان جداکننده اعداد لیست نیز استفاده می شود: الگوریتم تشخیص لیست برای شماره گذاری سبک عربی (1., 1.1.2.) از هر دو علامت فاصله و علامت نقطه (".") استفاده می کند. - اگر این گزینه روی
False
تنظیم شده باشد، الگوریتم تشخیص لیست، پاراگراف های لیست را تشخیص می دهد، زمانی که اعداد لیست با علامت های نقطه، براکت راست یا گلوله (مانند “•"، “*"، “-” یا “o”) پایان می یابد.
مثال کد زیر نحوه استفاده از این ویژگی را نشان می دهد:
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET | |
# Create a plaintext document in the form of a string with parts that may be interpreted as lists. | |
# Upon loading, the first three lists will always be detected by Aspose.words, | |
# and List objects will be created for them after loading. | |
textDoc = """Full stop delimiters:\n | |
1. First list item 1\n | |
2. First list item 2\n | |
3. First list item 3\n\n | |
Right bracket delimiters:\n | |
1) Second list item 1\n | |
2) Second list item 2\n | |
3) Second list item 3\n\n | |
Bullet delimiters:\n | |
• Third list item 1\n | |
• Third list item 2\n | |
• Third list item 3\n\n | |
Whitespace delimiters:\n | |
1 Fourth list item 1\n | |
2 Fourth list item 2\n | |
3 Fourth list item 3""" | |
# The fourth list, with whitespace inbetween the list number and list item contents, | |
# will only be detected as a list if "DetectNumberingWithWhitespaces" in a LoadOptions object is set to true, | |
# to avoid paragraphs that start with numbers being mistakenly detected as lists. | |
loadOptions = aw.loading.TxtLoadOptions() | |
loadOptions.detect_numbering_with_whitespaces = True | |
# Load the document while applying LoadOptions as a parameter and verify the result. | |
doc = aw.Document(io.BytesIO(textDoc.encode("utf-8")), loadOptions) | |
doc.save(docs_base.artifacts_dir + "WorkingWithTxtLoadOptions.detect_numbering_with_whitespaces.docx") |
در حین بارگذاری TXT، فضاهای پیشرو و انتهایی را مدیریت کنید
شما می توانید نحوه مدیریت فضاهای پیشرو و انتهایی را در حین بارگذاری فایل TXT کنترل کنید. فضاهای پیشرو را می توان تراش داد، حفظ کرد یا به تورفتگی تبدیل کرد و فضاهای انتهایی را می توان کوتاه یا حفظ کرد.
مثال کد زیر نشان می دهد که چگونه می توان فضاهای اصلی و انتهایی را هنگام وارد کردن فایل TXT برش داد:
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET | |
textDoc = " Line 1 \n" + " Line 2 \n" + " Line 3 " | |
loadOptions = aw.loading.TxtLoadOptions() | |
loadOptions.leading_spaces_options = aw.loading.TxtLeadingSpacesOptions.TRIM | |
loadOptions.trailing_spaces_options = aw.loading.TxtTrailingSpacesOptions.TRIM | |
f = io.BytesIO(textDoc.encode("utf-8")) | |
doc = aw.Document(f, loadOptions) | |
doc.save(docs_base.artifacts_dir + "WorkingWithTxtLoadOptions.handle_spaces_options.docx") |
تشخیص جهت متن سند
Aspose.Words ویژگی document_direction را در کلاس TxtLoadOptions برای تشخیص جهت متن (RTL / LTR) در سند ارائه می دهد. این ویژگی دستورالعمل های متن سند ارائه شده در شمارش DocumentDirection را تنظیم یا دریافت می کند. مقدار پیش فرض از چپ به راست است.
مثال کد زیر نحوه تشخیص جهت متن سند را در هنگام وارد کردن فایل TXT نشان می دهد:
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET | |
loadOptions = aw.loading.TxtLoadOptions() | |
loadOptions.document_direction = aw.loading.DocumentDirection.AUTO | |
doc = aw.Document(docs_base.my_dir + "Hebrew text.txt", loadOptions) | |
paragraph = doc.first_section.body.first_paragraph | |
print(paragraph.paragraph_format.bidi) | |
doc.save(docs_base.artifacts_dir + "WorkingWithTxtLoadOptions.document_text_direction.docx") |
هدر و پاورقی را در خروجی TXT صادر کنید
اگر می خواهید هدر و پاورقی را در سند خروجی TXT صادر کنید، می توانید از ویژگی export_headers_footers_mode استفاده کنید. این ویژگی نحوه صادرات سرصفحه ها و پاورقی ها به قالب متن ساده را مشخص می کند.
مثال کد زیر نحوه صادرات سرصفحه و پاورقی به قالب متن ساده را نشان می دهد:
doc = aw.Document(docs_base.my_dir + "Document.docx")
options = aw.saving.TxtSaveOptions()
options.save_format = aw.SaveFormat.TEXT
# All headers and footers are placed at the very end of the output document.
options.export_headers_footers_mode = aw.saving.TxtExportHeadersFootersMode.ALL_AT_END
doc.save(docs_base.artifacts_dir + "WorkingWithTxtSaveOptions.export_headers_footers_mode_A.txt", options)
# Only primary headers and footers are exported at the beginning and end of each section.
options.export_headers_footers_mode = aw.saving.TxtExportHeadersFootersMode.PRIMARY_ONLY
doc.save(docs_base.artifacts_dir + "WorkingWithTxtSaveOptions.export_headers_footers_mode_B.txt", options)
# No headers and footers are exported.
options.export_headers_footers_mode = aw.saving.TxtExportHeadersFootersMode.NONE
doc.save(docs_base.artifacts_dir + "WorkingWithTxtSaveOptions.export_headers_footers_mode_C.txt", options)
صادرات تورفتگی لیست در خروجی TXT
Aspose.Words کلاس TxtListIndentation را معرفی کرد که امکان تعیین نحوه تورفتگی سطوح لیست را در حین صادرات به قالب متن ساده فراهم می کند. در حین کار با TxtSaveOption، ویژگی list_indentation برای تعیین کاراکتر مورد استفاده برای تورفتگی سطوح لیست و تعداد کاراکترها به عنوان تورفتگی در هر سطح فهرست ارائه می شود. مقدار پیش فرض ویژگی کاراکتر ‘\0’ است که نشان می دهد هیچ تورفتگی وجود ندارد. برای ویژگی count، مقدار پیشفرض 0 است که به معنای عدم تورفتگی است.
استفاده از کاراکتر Tab
مثال کد زیر نحوه صادرات سطوح لیست را با استفاده از کاراکترهای برگه نشان می دهد:
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET | |
doc = aw.Document() | |
builder = aw.DocumentBuilder(doc) | |
# Create a list with three levels of indentation. | |
builder.list_format.apply_number_default() | |
builder.writeln("Item 1") | |
builder.list_format.list_indent() | |
builder.writeln("Item 2") | |
builder.list_format.list_indent() | |
builder.write("Item 3") | |
saveOptions = aw.saving.TxtSaveOptions() | |
saveOptions.list_indentation.count = 1 | |
#saveOptions.list_indentation.character = '\t' | |
doc.save(docs_base.artifacts_dir + "WorkingWithTxtSaveOptions.use_tab_character_per_level_for_list_indentation.txt", saveOptions) |
استفاده از کاراکتر فضایی
مثال کد زیر نحوه صادرات سطوح لیست را با استفاده از کاراکترهای فاصله نشان می دهد:
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET | |
doc = aw.Document() | |
builder = aw.DocumentBuilder(doc) | |
# Create a list with three levels of indentation. | |
builder.list_format.apply_number_default() | |
builder.writeln("Item 1") | |
builder.list_format.list_indent() | |
builder.writeln("Item 2") | |
builder.list_format.list_indent() | |
builder.write("Item 3") | |
saveOptions = aw.saving.TxtSaveOptions() | |
saveOptions.list_indentation.count = 3 | |
#saveOptions.list_indentation.character = ' ' | |
doc.save(docs_base.artifacts_dir + "WorkingWithTxtSaveOptions.use_space_character_per_level_for_list_indentation.txt", saveOptions) |