ทำงานกับข้อความในตาราง
ตามที่กล่าวไว้ในบทความก่อนหน้านี้ ตารางมักจะมีข้อความธรรมดา แม้ว่าเนื้อหาอื่นๆ เช่น รูปภาพ หรือแม้แต่ตารางอื่นๆ ก็สามารถวางในเซลล์ตารางได้
การเพิ่มข้อความหรือเนื้อหาอื่นๆ ลงในตารางทำได้โดยใช้วิธีการที่เหมาะสมของคลาส DocumentBuilder และอธิบายไว้ในบทความ “สร้างตาราง” ในบทความนี้ เราจะพูดถึงวิธีทำงานกับข้อความในตารางที่มีอยู่แล้ว
แทนที่ข้อความในตาราง
ตารางก็เหมือนกับโหนดอื่นๆ ใน Aspose.Words ที่สามารถเข้าถึงออบเจ็กต์ Range เมื่อใช้วัตถุช่วงตาราง คุณสามารถแทนที่ข้อความในตารางได้
ขณะนี้รองรับความสามารถในการใช้อักขระพิเศษเมื่อแทนที่ ดังนั้นจึงเป็นไปได้ที่จะแทนที่ข้อความที่มีอยู่ด้วยข้อความหลายย่อหน้า ในการดำเนินการนี้ คุณจะต้องใช้อักขระเมตาพิเศษที่อธิบายไว้ในวิธี Replace ที่เกี่ยวข้อง
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการแทนที่อินสแตนซ์ทั้งหมดของสตริงข้อความในเซลล์ของทั้งตาราง:
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET.git. | |
doc = aw.Document(MY_DIR + "Tables.docx") | |
table = doc.get_child(aw.NodeType.TABLE, 0, True).as_table() | |
table.range.replace("Carrots", "Eggs", aw.replacing.FindReplaceOptions(aw.replacing.FindReplaceDirection.FORWARD)) | |
table.last_row.last_cell.range.replace("50", "20", aw.replacing.FindReplaceOptions(aw.replacing.FindReplaceDirection.FORWARD)) | |
doc.save(ARTIFACTS_DIR + "FindAndReplace.replace_text_in_table.docx") |
แยกข้อความธรรมดาจากตารางหรือเซลล์
เมื่อใช้ออบเจ็กต์ Range คุณยังสามารถเรียกใช้เมธอดบนช่วงตารางทั้งหมดและแยกตารางเป็นข้อความธรรมดาได้ เมื่อต้องการทำเช่นนี้ ให้ใช้คุณสมบัติ Text
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการพิมพ์ช่วงข้อความของตาราง:
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET.git. | |
doc = aw.Document(MY_DIR + "Tables.docx") | |
table = doc.get_child(aw.NodeType.TABLE, 0, True).as_table() | |
# The range text will include control characters such as "\a" for a cell. | |
# You can call ToString and pass SaveFormat.text on the desired node to find the plain text content. | |
print("Contents of the table: ") | |
print(table.range.text) |
เทคนิคเดียวกันนี้ใช้เพื่อแยกเนื้อหาจากเซลล์ตารางแต่ละเซลล์เท่านั้น
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการพิมพ์ช่วงข้อความขององค์ประกอบแถวและตาราง:
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET.git. | |
print("\nContents of the row: ") | |
print(table.rows[1].range.text) | |
print("\nContents of the cell: ") | |
print(table.last_row.last_cell.range.text) |
การทำงานกับข้อความตารางทางเลือก
ตาราง Microsoft Word มี table title
และ table description
ที่ให้ทางเลือกในการแสดงข้อความที่มีอยู่ในตาราง
ใน Aspose.Words คุณยังเพิ่มชื่อตารางและคำอธิบายโดยใช้คุณสมบัติ Title และ Description ได้ด้วย คุณสมบัติเหล่านี้มีความหมายสำหรับเอกสาร DOCX ที่สอดคล้องกับ ISO/IEC 29500 เมื่อบันทึกในรูปแบบที่เก่ากว่า ISO/IEC 29500 คุณสมบัติเหล่านี้จะถูกละเว้น
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการตั้งค่าชื่อเรื่องและคำอธิบายคุณสมบัติของตาราง:
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET.git. | |
doc = aw.Document(MY_DIR + "Tables.docx") | |
table = doc.get_child(aw.NodeType.TABLE, 0, True).as_table() | |
table.title = "Test title" | |
table.description = "Test description" | |
options = aw.saving.OoxmlSaveOptions() | |
options.compliance = aw.saving.OoxmlCompliance.ISO29500_2008_STRICT | |
doc.compatibility_options.optimize_for(aw.settings.MsWordVersion.WORD2016) | |
doc.save(ARTIFACTS_DIR + "WorkingWithTableStylesAndFormatting.table_title_and_description.docx", options) |