สร้าง TOC ใน Word

สารบัญ (TOC) เป็นฟีเจอร์ที่สำคัญในเอกสาร Word โดยเฉพาะเอกสารที่มีความยาว เช่น รายงาน, วิทยานิพนธ์, และ eBooks มันทำให้เอกสาร Word อ่านง่ายขึ้นและช่วยให้ผู้อ่านสามารถนำทางไปยังส่วนต่างๆ ของเอกสารได้อย่างรวดเร็ว ในโพสต์นี้คุณจะได้เรียนรู้วิธีการทำให้สารบัญ (TOC) ในเอกสาร Word อัตโนมัติด้วย Python เราจะพูดถึงขั้นตอนในการสร้างสารบัญในเอกสาร Word, การแทรก TOC ในเอกสารที่มีอยู่, หรือการดึง TOC จากเอกสารใน Python

บทความนี้ครอบคลุมหัวข้อดังต่อไปนี้:

ไลบรารี Python Word สำหรับการทำงานกับ TOC

ในการทำงานกับ TOC ในเอกสาร Word โดยใช้ Python เราจะใช้ Aspose.Words for Python ไลบรารี มันช่วยให้ผู้พัฒนาสามารถสร้าง, แก้ไข, และทำให้เอกสาร Word อัตโนมัติในแอปพลิเคชัน Python ของคุณ เริ่มต้นด้วยการตั้งค่าสภาพแวดล้อมของเรา

ก่อนที่เราจะลงลึกในโค้ดให้แน่ใจว่าคุณได้ติดตั้ง Aspose.Words for Python แล้ว คุณสามารถ ดาวน์โหลดแพ็กเกจ หรือ ติดตั้ง API จาก PyPI โดยใช้คำสั่ง pip ต่อไปนี้ในเทอร์มินัลของคุณ:

pip install aspose-words

นี่จะดาวน์โหลดและติดตั้งไลบรารี Aspose.Words for Python และความต้องการพื้นฐานของมัน

สร้างสารบัญใน Word โดยใช้ Python

โปรดทำตามขั้นตอนด้านล่างเพื่อเพิ่มสารบัญในเอกสาร Word โดยใช้ Python

  1. สร้างออบเจ็กต์ของคลาส Document
  2. สร้างออบเจ็กต์ของคลาส DocumentBuilder โดยใช้วัตถุ Document
  3. แทรกสารบัญโดยใช้เมธอด insert_table_of_contents()
  4. กำหนดสไตล์หัวข้อโดยใช้คุณสมบัติ style_identifier
  5. อัปเดตฟิลด์โดยใช้เมธอด update_fields()
  6. บันทึกเอกสาร Word โดยใช้เมธอด save()

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการสร้างสารบัญในเอกสาร Word โดยใช้ Python

# This code example shows how to add a Table of Contents in a Word document.
# Create a document
doc = aw.Document()
# Initialize a document builder
builder = aw.DocumentBuilder(doc)
# Insert table of contents
builder.insert_table_of_contents("\\o \"1-3\" \\h \\z \\u")
# Start the actual document content on the second page.
builder.insert_break(aw.BreakType.PAGE_BREAK)
builder.paragraph_format.style_identifier = aw.StyleIdentifier.HEADING1
builder.writeln("Heading 1")
builder.paragraph_format.style_identifier = aw.StyleIdentifier.HEADING2
builder.writeln("Heading 1.1")
builder.writeln("Heading 1.2")
builder.paragraph_format.style_identifier = aw.StyleIdentifier.HEADING1
builder.writeln("Heading 2")
builder.writeln("Heading 3")
builder.paragraph_format.style_identifier = aw.StyleIdentifier.HEADING2
builder.writeln("Heading 3.1")
builder.paragraph_format.style_identifier = aw.StyleIdentifier.HEADING3
builder.writeln("Heading 3.1.1")
builder.writeln("Heading 3.1.2")
builder.writeln("Heading 3.1.3")
builder.paragraph_format.style_identifier = aw.StyleIdentifier.HEADING2
builder.writeln("Heading 3.2")
builder.writeln("Heading 3.3")
# The newly inserted table of contents will be initially empty.
# It needs to be populated by updating the fields in the document.
doc.update_fields()
# Save the document
doc.save("insert_table_of_contents.docx")
สร้างสารบัญใน Word โดยใช้ Python

เพิ่มสารบัญใน Word โดยใช้ Python

สร้างสารบัญในเอกสาร Word ที่มีอยู่

คุณยังสามารถแทรกสารบัญในเอกสาร Word ที่มีอยู่โดยทำตามขั้นตอนด้านล่าง:

  1. โหลดเอกสาร Word ที่มีอยู่โดยใช้คลาส Document
  2. สร้างออบเจ็กต์ของคลาส DocumentBuilder และเริ่มต้นด้วยวัตถุ Document ที่สร้างขึ้นก่อนหน้า
  3. แทรกสารบัญโดยใช้เมธอด insert_table_of_contents()
  4. อัปเดตฟิลด์โดยใช้เมธอด update_fields()
  5. บันทึกเอกสาร Word โดยใช้เมธอด save()

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการสร้างสารบัญในเอกสาร Word ที่มีอยู่โดยใช้ Python

# This code example shows how to insert a Table of Contents in an existing Word document.
# Load an existing Word document
doc = Document("toc_sample.docx");
builder = DocumentBuilder(doc);
# Insert a table of contents at the beginning of the document.
builder.insert_table_of_contents("\\o \"1-3\" \\h \\z \\u");
# The newly inserted table of contents will be initially empty.
# It needs to be populated by updating the fields in the document.
doc.update_fields();
# Save the document
doc.save("InsertTOC_out.docx");
สร้างสารบัญในเอกสาร Word ที่มีอยู่

สร้างสารบัญในเอกสาร Word ที่มีอยู่

ดึงสารบัญจากเอกสาร Word ด้วย Python

คุณสามารถดึงฟิลด์จากสารบัญของเอกสาร Word โดยทำตามขั้นตอนด้านล่าง:

  1. โหลดเอกสาร Word โดยใช้คลาส Document
  2. วนลูปผ่านคอลเลกชัน doc.range.fields
  3. ตรวจสอบว่าประเภทของฟิลด์คือ FIELD_HYPERLINK
  4. ตรวจสอบว่าฟิลด์นั้นอยู่ในส่วนของสารบัญ
  5. ดึงข้อมูลจากฟิลด์และพิมพ์มัน

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการดึงสารบัญจากเอกสาร Word โดยใช้ Python

# This code example shows how to extract a Table of Contents from a Word document.
# Load an existing Word document
doc = aw.Document(InsertTOC_out.docx")
# Loop through all the fields
for field in doc.range.fields:
# Check if it is FIELD_HYPERLINK
if (field.type == aw.fields.FieldType.FIELD_HYPERLINK):
hyperlink = field.as_field_hyperlink()
# Check if it in TOC section
if (hyperlink.sub_address != None and hyperlink.sub_address.find("_Toc") == 0):
tocItem = field.start.get_ancestor(aw.NodeType.PARAGRAPH).as_paragraph()
print(tocItem.to_string(aw.SaveFormat.TEXT).strip())
print("------------------")
# Print
bm = doc.range.bookmarks.get_by_name(hyperlink.sub_address)
pointer = bm.bookmark_start.get_ancestor(aw.NodeType.PARAGRAPH).as_paragraph()
print(pointer.to_string(aw.SaveFormat.TEXT))
หัวข้อ 1       1
------------------
หัวข้อ 1

หัวข้อ 2        1
------------------
หัวข้อ 2

หัวข้ออื่น 1
------------------
หัวข้ออื่น

ขอใบอนุญาตฟรี

คุณสามารถ ขอใบอนุญาตชั่วคราวฟรี และสร้างสารบัญในเอกสาร Word โดยไม่มีข้อจำกัดการประเมิน

TOC ใน Word – ทรัพยากรฟรี

  • อ่าน การทำงานกับสารบัญ ส่วนของเอกสารอย่างเป็นทางการเพื่อเรียนรู้เพิ่มเติมเกี่ยวกับการเพิ่ม, อัปเดต, หรือการลบ TOC ใน Word

นอกเหนือจากการทำงานกับ TOC ในเอกสาร Word ค้นพบฟีเจอร์เพิ่มเติมของ Aspose.Words for Python โดยใช้ทรัพยากรด้านล่าง:

สรุปใน

บทความนี้คุณได้เรียนรู้วิธีการทำงานกับสารบัญ (TOC) ในเอกสาร Word โดยอัตโนมัติ ด้วยการทำตามขั้นตอนที่กล่าวถึงในบทความนี้ คุณสามารถทำให้กระบวนการสร้างและจัดการเอกสารที่ยาวนานเป็นอัตโนมัติ Aspose.Words for Python ช่วยให้คุณจัดการเอกสาร Word ของคุณอย่างมีประสิทธิภาพและเพิ่มการใช้งานของมันด้วยสารบัญที่มีโครงสร้างดี หากคุณมีคำถามหรือความต้องการเพิ่มเติมกรุณาติดต่อเราที่ ฟอรัมสนับสนุนฟรี

ดูเพิ่มเติม