ภาพรวม
การรวมเอกสาร Word เป็นความต้องการทั่วไปในหลายแอปพลิเคชันไม่ว่าจะเพื่อการรวมรายงาน การรวบรวมข้อมูล หรือแค่ทำให้การจัดการเอกสารมีความสะดวกขึ้น ในบทความนี้เราจะสำรวจวิธีการรวมไฟล์ DOCX โดยใช้ Python กับไลบรารี GroupDocs.Merger ไลบรารีที่ทรงพลังนี้ช่วยให้นักพัฒนาสามารถจัดการเอกสาร Word ได้อย่างง่ายดายผ่านโปรแกรมโดยมั่นใจในประสิทธิภาพและความแม่นยำสูง
คู่มือนี้จะครอบคลุมต่อไปนี้:
- วิธีการรวมไฟล์ DOCX หลายไฟล์
- วิธีการรวมเอกสารโดยไม่มีการแบ่งส่วน
- วิธีการรวมด้วยโหมดการปฏิบัติตามที่กำหนดไว้ล่วงหน้า
- การจัดการสตรีมเอกสารใน Python
วิธีการรวมไฟล์ DOCX หลายไฟล์
การรวมไฟล์ DOCX หลายไฟล์นั้นไม่ยุ่งยากด้วยไลบรารี GroupDocs.Merger ด้านล่างนี้เราจะสรุปขั้นตอนสำคัญที่เกี่ยวข้องในกระบวนการนี้:
- นำเข้าแพ็กเกจ GroupDocs.Merger: เริ่มต้นโดยการนำเข้าไลบรารีที่จำเป็น
- สร้างอินสแตนซ์ Merger: สร้างวัตถุ Merger โดยใช้พาธของไฟล์ DOCX ตัวแรก
- เข้าร่วมเอกสารเพิ่มเติม: ใช้เมธอด
join
เพื่อเพิ่มไฟล์ DOCX อื่นที่คุณต้องการรวม - บันทึกเอกสารที่รวม: สุดท้ายเรียกใช้เมธอด
save
เพื่อเขียนเอกสารที่รวมไปยังพาธที่กำหนดไว้
นี่คือตัวอย่างโค้ด Python ที่แสดงขั้นตอนเหล่านี้:
import groupdocs.merger as gm
import constants
def run():
print(f"----------------------------------------------------------------------------")
print(f"[Example Basic Usage] # MultipleDocumentOperations # Merge : Docx")
# Step 1: Create a Merger object with the first document
with gm.Merger(constants.sample_docx) as merger:
print(f"Document info retrieved successfully")
# Step 2: Join another DOCX file to merge
merger.join(constants.sample_docx)
# Step 3: Save the merged document
merger.save(constants.output_docx)
print(f"Merge to: {constants.output_docx}")
print(f"----------------------------------------------------------------------------")
รวมเอกสาร DOCX โดยไม่มีการแบ่งส่วน
บางครั้งการรวมเอกสารอาจต้องการให้เนื้อหาถูกเข้าร่วมโดยไม่มีการแทรกการแบ่งส่วน ซึ่งสามารถช่วยรักษาการไหลของข้อมูลข้ามเอกสารที่รวม
- สร้างอินสแตนซ์ Merger: เช่นเดียวกับก่อนหน้า เริ่มต้นโดยการสร้างคลาส Merger
- ตั้งค่าตัวเลือกการเข้าร่วม: กำหนด
WordJoinOptions
และตั้งค่าmode
เป็นDISABLE_SECTION_BREAKS
- เข้าร่วมเอกสาร: เพิ่มเอกสารเพิ่มเติมเพื่อรวม
- บันทึกผลลัพธ์: เขียนเอาต์พุตสุดท้ายไปยังไฟล์ DOCX ใหม่
นี่คือวิธีที่คุณสามารถทำได้:
import groupdocs.merger as gm
import constants
def run():
print(f"----------------------------------------------------------------------------")
print(f"[Example Basic Usage] # MultipleDocumentOperations # Merge # Word : MergeWordDocumentsWithoutSectionBreaks")
# Step 1: Create a Merger object with the source document
with gm.Merger(constants.sample_doc) as merger:
print(f"Document info retrieved successfully")
# Step 2: Define Word join options to disable section breaks
word_join_options = gm.domain.options.WordJoinOptions()
word_join_options.mode = gm.domain.options.WordJoinMode.DISABLE_SECTION_BREAKS
# Step 3: Join the document without section breaks
merger.join(constants.sample_doc, word_join_options)
# Step 4: Save the merged document
merger.save(constants.output_doc_without_section_breaks)
print(f"Merge to: {constants.output_doc_without_section_breaks}")
print(f"----------------------------------------------------------------------------")
รวมเอกสาร DOCX ด้วยโหมดการปฏิบัติตามที่กำหนดไว้ล่วงหน้า
สำหรับการใช้งานเฉพาะบางอย่าง เอกสารอาจต้องปฏิบัติตามมาตรฐานที่กำหนดไว้ GroupDocs.Merger ช่วยให้คุณรวมเอกสารด้วยโหมดการปฏิบัติตามที่กำหนดไว้ล่วงหน้า เพื่อให้แน่ใจว่าเอาต์พุตตรงตามมาตรฐาน ISO
- สร้างอินสแตนซ์ Merger: เริ่มต้นคลาส Merger ด้วยเอกสารฐานของคุณ
- ตั้งค่าโหมดการปฏิบัติตาม: สร้างวัตถุ
WordJoinOptions
และตั้งค่าcompliance
- เข้าร่วมเอกสาร: เพิ่มเอกสารเพิ่มเติมโดยใช้เมธอด join
- บันทึกเอกสารสุดท้าย: บันทึกไปยังตำแหน่งที่คุณต้องการ
นี่คือโค้ดสำหรับการดำเนินการนี้:
import groupdocs.merger as gm
import constants
def run():
print(f"----------------------------------------------------------------------------")
print(f"[Example Basic Usage] # MultipleDocumentOperations # Merge # Word : MergeWordDocumentsWithPredefinedComplianceMode")
# Step 1: Create a Merger object with the document
with gm.Merger(constants.sample_docx) as merger:
print(f"Document info retrieved successfully")
# Step 2: Define Word join options with predefined compliance mode
word_join_options = gm.domain.options.WordJoinOptions()
word_join_options.compliance = gm.domain.options.WordJoinCompliance.ISO_29500_2008_STRICT
# Step 3: Join another document with compliance mode
merger.join(constants.sample_docx, word_join_options)
# Step 4: Save the merged document
merger.save(constants.output_docx_with_predefined_compliance_mode)
print(f"Merge to: {constants.output_docx_with_predefined_compliance_mode}")
print(f"----------------------------------------------------------------------------")
วิธีการรวม DOCX จากสตรีม
คุณยังสามารถรวมไฟล์ DOCX จากสตรีมในหน่วยความจำ ซึ่งเป็นประโยชน์เมื่อคุณจัดการกับเอกสารที่สร้างขึ้นแบบไดนามิก
- รับสตรีมไฟล์: เปิดเอกสารของคุณในโหมดไบนารีเพื่อให้ได้รับสตรีม
- สร้างอินสแตนซ์ Merger: สร้างคลาส
Merger
โดยใช้สตรีม - ดำเนินการรวม: รวมตามต้องการและบันทึกผล
นี่คือวิธีที่คุณจะดำเนินการ:
from turtle import update
import groupdocs.merger as gm
import constants
def run():
print(f"----------------------------------------------------------------------------")
print(f"[Example Advanced Usage] # Loading # LoadDocumentFromStream")
# Step 1: Get the document stream
stream = get_file_stream()
# Step 2: Create a Merger instance using the stream
with gm.Merger(stream) as merger:
print(f"Document loaded from stream successfully")
print(f"----------------------------------------------------------------------------")
def get_file_stream():
file_path = constants.sample_docx
return open(file_path, "rb")
ดูเพิ่มเติม
สำหรับข้อมูลเพิ่มเติม คุณสามารถสำรวจแหล่งข้อมูลต่อไปนี้:
- เอกสาร GroupDocs.Merger สำหรับ Python
- เอกสารอ้างอิงสำหรับ GroupDocs.Merger
- ตัวอย่าง GitHub สำหรับ GroupDocs.Merger
คุณยังสามารถดาวน์โหลดเวอร์ชันทดลองใช้งานได้ฟรีจาก releases.groupdocs.com และขอใบอนุญาตชั่วคราวเพื่อทดลองใช้ไลบรารีนี้โดยไม่มีข้อจำกัดที่ ซื้อใบอนุญาตชั่วคราว.
ตัวอย่างโค้ด
นี่คือตัวอย่างเพิ่มเติมเพื่อช่วยให้คุณเข้าใจขั้นตอนการรวมด้วยไลบรารี GroupDocs.Merger สำหรับ Python:
รวมเอกสาร Word โดยไม่เริ่มจากหน้ากระดาษใหม่
ตัวอย่างนี้แสดงวิธีการรวมเอกสารโดยให้หน้าสุดท้ายของเอกสารแรกตามหลังหน้าของเอกสารถัดไปโดยตรง โดยไม่มีหน้ากระดาษใหม่แทรกอยู่ระหว่าง
import groupdocs.merger as gm
import constants
def run():
print(f"----------------------------------------------------------------------------")
print(f"[Example Basic Usage] # MultipleDocumentOperations # Merge # Word : MergeWordDocumentsWithoutStartingFromNewPage")
# Step 1: Create a Merger object with the document
with gm.Merger(constants.sample_doc) as merger:
print(f"Document info retrieved successfully")
# Step 2: Define Word join options for continuous mode
word_join_options = gm.domain.options.WordJoinOptions()
word_join_options.mode = gm.domain.options.WordJoinMode.CONTINUOUS
# Step 3: Join documents without inserting new page
merger.join(constants.sample_doc, word_join_options)
# Step 4: Save the merged document
merger.save(constants.output_doc_without_starting_from_new_page)
print(f"Merge to: {constants.output_doc_without_starting_from_new_page}")
print(f"----------------------------------------------------------------------------")
รวมเอกสารหลายไฟล์ด้วยตัวเลือกการเข้าร่วมที่กำหนด
นี่คือวิธีการรวมไฟล์ DOCX ขณะที่ตั้งค่าตัวเลือกการเข้าร่วมเฉพาะ เช่น การปฏิบัติตามมาตรฐาน ISO ที่กำหนด
import groupdocs.merger as gm
import constants
def run():
print(f"----------------------------------------------------------------------------")
print(f"[Example Advanced Usage] # Merge with Custom Join Options")
# Step 1: Create a Merger object with the base document
with gm.Merger(constants.sample_docx) as merger:
print(f"Document info retrieved successfully")
# Step 2: Set custom join options for predefined compliance mode
word_join_options = gm.domain.options.WordJoinOptions()
word_join_options.compliance = gm.domain.options.WordJoinCompliance.ISO_29500_2008_STRICT
# Step 3: Join another document with compliance settings
merger.join(constants.sample_docx, word_join_options)
# Step 4: Save the merged document
merger.save(constants.output_docx_with_predefined_compliance_mode)
print(f"Merge to: {constants.output_docx_with_predefined_compliance_mode}")
print(f"----------------------------------------------------------------------------")
ดูเพิ่มเติม
เพื่อสำรวจเพิ่มเติมเกี่ยวกับ GroupDocs.Merger และฟังก์ชันการทำงานของมัน คุณสามารถตรวจสอบแหล่งข้อมูลต่อไปนี้:
- เอกสาร GroupDocs.Merger สำหรับ Python
- เอกสารอ้างอิงสำหรับ GroupDocs.Merger
- ตัวอย่างโค้ด GitHub สำหรับ GroupDocs.Merger
- หน้า Releases สำหรับ GroupDocs.Merger
เวอร์ชันทดลองและใบอนุญาตชั่วคราว
คุณสามารถดาวน์โหลดเวอร์ชันทดลองของ GroupDocs.Merger จาก หน้าปล่อย ของเรา นอกจากนี้ คุณสามารถขอใบอนุญาตชั่วคราวเพื่อการประเมินที่ ซื้อใบอนุญาตชั่วคราว ใบอนุญาตชั่วคราวนี้ช่วยให้คุณสามารถทดลองใช้ไลบรารีนี้ได้โดยไม่มีข้อจำกัดและประเมินความสามารถอย่างเต็มที่