この記事では、Python を使用して Word の空白ページを削除する方法について説明します。環境設定の詳細、手順のリスト、Python を使用して Word の空のページを削除する方法 を示す実行可能なサンプル コードが含まれています。サンプル コードをそのまま使用してタスクを実行できますが、必要に応じて別の条件を追加して即興でコードを作成することもできます。
Python を使用して Word の空白ページを削除する手順
- .NET 経由の Python 用 Aspose.Words を使用して空白ページを削除するように開発環境を設定します
- 空白ページが含まれる Document オブジェクトにソース Word ファイルをロードします
- 文書内のすべてのページを繰り返し処理し、各セクションの内容を確認します。
- テキストや画像の有無を確認し、空ページのリストを作成する
- 新しい空の Word 文書を作成する
- Append 空白ページ リストを使用して、ソース Word ファイルからコンテンツが含まれるすべてのページを取得します
- 空白ページのない Word ファイルを保存します。
これらの手順では、Python を使用して空白の Word ページを削除する方法 のプロセスを説明します。このプロセスでは、ソース Word ファイルがロードされ、各ページのすべてのセクションにテキストまたは画像が含まれているかどうかが検査され、空白ページのリストが作成されます。最終的に、新しいドキュメントが作成され、空白ページを除くすべてのページがこの新しい空のドキュメントに追加されます。
Python を使用して Word の空白ページを削除するコード
import aspose.words as aw | |
import aspose.pydrawing as drawing | |
# Load the license | |
wordLic = aw.License() | |
wordLic.set_license("License.lic") | |
# Load the Word file having blank pages in it | |
originalDocWithFewBlankPages = aw.Document("input.docx") | |
# Declare an array for blank page numbers | |
listOfBlankPageNumbers = [] | |
listOfBlankPageNumbers.append(-1) | |
# Get total pages in the souce Word file | |
totalPagesInOriginalDoc = originalDocWithFewBlankPages.page_count | |
for iCount in range(0, totalPagesInOriginalDoc): # This will loop for page count | |
# Extract each page one by one | |
DocWithOnePage = originalDocWithFewBlankPages.extract_pages(iCount, 1) | |
shapesCounter = 0 | |
pageText = "" | |
# Check text and images in each section | |
for docSection in DocWithOnePage.sections: | |
docSection = docSection.as_section() | |
pageText = pageText + docSection.body.to_string(aw.SaveFormat.TEXT) | |
shapesCounter += docSection.body.get_child_nodes(aw.NodeType.SHAPE, True).count | |
# Check if no content is there | |
if (not pageText) and shapesCounter == 0: | |
# Store the index of blank page numbers | |
listOfBlankPageNumbers.append(iCount) | |
listOfBlankPageNumbers.append(totalPagesInOriginalDoc) | |
# Create a single page document with default forrmat | |
nonEmptyDocument = originalDocWithFewBlankPages.clone(False).as_document() | |
# Clean the document | |
nonEmptyDocument.remove_all_children() | |
# Append the pages with content to the new document | |
for iCount in range(1, len(listOfBlankPageNumbers)): | |
index = listOfBlankPageNumbers[iCount - 1] + 1 | |
count = listOfBlankPageNumbers[iCount] - index | |
if count > 0: | |
nonEmptyDocument.append_document( | |
originalDocWithFewBlankPages.extract_pages(index, count), | |
aw.ImportFormatMode.KEEP_SOURCE_FORMATTING) | |
# Save the document having some content in it | |
nonEmptyDocument.save("NonEmptyPages.docx") | |
print ("Blank pages deleted successfully") |
このサンプル コードは、Python を使用して Word から空のページを削除する方法 を示します。このサンプル コードでは、セクションごとに本文テキストとノード タイプ SHAPE がテストされていますが、空白ページを識別するために TABLE、COMMENT、FORM_FIELD、SMART_TAG などの他のタイプを使用することもできます。 append_document() メソッドは、新しい Word ファイルにページを追加するために使用されますが、このメソッドには、ページ挿入プロセスをカスタマイズするためのさまざまな引数を許可するさまざまなオーバーロードされたメソッドが含まれています。
この記事では、Python を使用して Word の空のページを削除する方法 を説明しました。 Word ファイルにページ番号を挿入する場合は、Pythonを使用してWordにページ番号を入力する方法 の記事を参照してください。