Python을 사용하여 Word에서 빈 페이지를 제거하는 방법

이 문서에서는 Python을 사용하여 Word에서 빈 페이지를 제거하는 방법을 설명합니다. 여기에는 환경 설정에 대한 세부 정보, 단계 목록 및 Python을 사용하여 Word에서 빈 페이지를 삭제하는 방법을 보여주는 실행 가능한 샘플 코드가 포함되어 있습니다. 샘플 코드는 작업을 수행하는 데 그대로 사용할 수 있지만 필요한 경우 다른 조건을 추가하여 즉석에서 만들 수도 있습니다.

Python을 사용하여 Word에서 빈 페이지를 삭제하는 단계

  1. 빈 페이지를 삭제하려면 .NET을 통한 Python용 Aspose.Words을 사용하도록 개발 환경을 설정하세요.
  2. 빈 페이지가 있는 Document 개체에 소스 Word 파일을 로드합니다.
  3. 문서의 모든 페이지를 반복하고 각 섹션의 내용을 확인합니다.
  4. 텍스트와 이미지 유무를 확인하고 빈 페이지 목록을 준비합니다.
  5. 새 빈 Word 문서 만들기
  6. Append 빈 페이지 목록을 사용하여 소스 Word 파일의 일부 콘텐츠가 있는 모든 페이지
  7. 빈 페이지가 없는 결과 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에 페이지 번호를 입력하는 방법의 문서를 참조하세요.

 한국인