การทำงานกับเอกสารข้อความ

ในบทความนี้ เราจะเรียนรู้ว่าตัวเลือกใดบ้างที่มีประโยชน์สำหรับการทำงานกับเอกสารข้อความผ่าน Aspose.Words โปรดทราบว่านี่ไม่ใช่รายการตัวเลือกทั้งหมดที่มีอยู่ แต่เป็นเพียงตัวอย่างการทำงานกับตัวเลือกบางส่วนเท่านั้น

เพิ่มเครื่องหมายสองทิศทาง

คุณสามารถใช้คุณสมบัติ add_bidi_marks เพื่อระบุว่าจะเพิ่มเครื่องหมายสองทิศทางก่อนที่ BiDi แต่ละตัวจะทำงานหรือไม่เมื่อส่งออกในรูปแบบข้อความธรรมดา Aspose.Words แทรกอักขระ Unicode ‘เครื่องหมายจากขวาไปซ้าย’ (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’ ซึ่งบ่งชี้ว่าไม่มีการเยื้อง สำหรับคุณสมบัติการนับ ค่าเริ่มต้นคือ 0 ซึ่งหมายถึงไม่มีการเยื้อง

การใช้อักขระแท็บ

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการส่งออกระดับรายการโดยใช้อักขระแท็บ:

# 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)