概要
Word文書の結合は、多くのアプリケーションで一般的な要件です。レポートの統合、情報の集約、または単に文書管理を簡素化するために必要です。この記事では、Pythonを使用してGroupDocs.MergerライブラリでDOCXファイルを結合する方法を探ります。この強力なライブラリは、開発者が高いパフォーマンスと精度を保ちながらプログラム的にWord文書を簡単に操作できるようにします。
このガイドでは以下の内容をカバーします:
複数のDOCXファイルを結合する方法
GroupDocs.Mergerライブラリを使用すると、複数のDOCXファイルを簡単に結合できます。下記にこのプロセスに関わる主要なステップを概説します:
- GroupDocs.Mergerパッケージをインポートする:必要なライブラリをインポートします。
- Mergerインスタンスを作成する:最初のDOCXファイルのパスを使用してMergerクラスをインスタンス化します。
- 追加の文書を結合する:
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 for Python Documentation
- API Reference for GroupDocs.Merger
- GroupDocs.Merger GitHub Examples
また、releases.groupdocs.comから無料トライアルをダウンロードし、Purchase Temporary Licenseで一時ライセンスを取得して、制限なしでライブラリを試すことができます。
コード例
以下は、GroupDocs.Mergerライブラリを使用した結合プロセスを理解するための追加の例です:
新しいページから始めずに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"----------------------------------------------------------------------------")
カスタム結合オプションで複数の文書を結合する
以下は、特定のISO標準に準拠するなど、特定の結合オプションを設定しながらDOCX文書を結合する方法です。
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 for Python Documentation
- API Reference for GroupDocs.Merger
- GitHub Code Examples for GroupDocs.Merger
- GroupDocs.Mergerのリリースページ
無料トライアルと一時ライセンス
GroupDocs.Mergerの無料トライアルは、リリースページからダウンロードできます。また、評価目的でPurchase Temporary Licenseで一時ライセンスを取得できます。この一時ライセンスを使用すると、制限なしでライブラリを試すことができ、その機能を完全に評価できます。