파이썬으로 PDF 파일 병합

파이썬에서 여러 PDF를 단일 PDF로 병합 또는 결합

PDF 파일을 결합하는 것은 사용자들 사이에서 매우 인기 있는 쿼리입니다. 이는 여러 PDF 파일을 하나의 문서로 공유하거나 함께 저장하려는 경우에 유용할 수 있습니다.

PDF 파일을 병합하면 문서를 구성하고, PC에 저장할 공간을 확보하고, 여러 PDF 파일을 하나의 문서로 결합하여 다른 사람들과 공유할 수 있습니다.

.NET을 통해 Python에서 PDF를 병합하는 것은 타사 라이브러리를 사용하지 않고는 간단한 작업이 아닙니다. 이 문서에서는.NET을 통해 Python용 Aspose.PDF 를 사용하여 여러 PDF 파일을 단일 PDF 문서로 병합하는 방법을 보여줍니다.

파이썬과 DOM을 사용하여 PDF 파일 병합

두 PDF 파일을 연결하려면:

  1. 새 문서 만들기.
  2. PDF 파일 병합
  3. 병합된 문서 저장

여러 PDF 문서를 단일 파일로 결합:

import sys
import aspose.pdf as ap
from os import path


def merge_two_documents(infile1, infile2, outfile):
    document1 = ap.Document(infile1)
    document2 = ap.Document(infile2)
    document1.pages.add(document2.pages)
    document1.save(outfile)

한 PDF에서 다른 PDF로 페이지 범위 추가

Python용 Aspose.PDF 를 사용하여 소스 PDF 문서의 특정 페이지 범위를 대상 PDF 문서에 복사하고 추가합니다.

  1. 문서 클래스를 사용하여 PDF 파일을 엽니다.
  2. 원본 문서에 페이지가 있는지 확인합니다.
  3. 페이지 범위를 확인합니다.
  4. 시작 페이지가 종료 페이지보다 큰 경우 작업을 건너뛰십시오.
  5. 페이지 범위를 반복하세요.
  6. 대상 문서에 페이지를 추가합니다.
import sys
import aspose.pdf as ap
from os import path


def _append_page_range(source_document, destination_document, start_page, end_page):
    total_pages = len(source_document.pages)
    if total_pages == 0:
        return

    start = max(1, start_page)
    end = min(end_page, total_pages)
    if start > end:
        return

    for page_number in range(start, end + 1):
        destination_document.pages.add(source_document.pages[page_number])

여러 PDF 문서를 하나로 병합

이 코드 스니펫은 여러 PDF 파일을 단일 문서로 병합하는 방법을 설명합니다.

  1. 빈 출력 문서를 생성합니다.
  2. 입력 파일을 반복합니다.
  3. 각 소스 문서를 로드합니다.
  4. 페이지 범위를 결정합니다.
  5. 출력 문서에 페이지를 추가합니다.
  6. 모든 문서에 대해 이 작업을 반복합니다.
  7. 병합된 PDF를 저장합니다.
import sys
import aspose.pdf as ap
from os import path


def merge_multiple_documents(input_files, outfile):
    output_document = ap.Document()

    for input_file in input_files:
        source_document = ap.Document(input_file)
        _append_page_range(
            source_document, output_document, 1, len(source_document.pages)
        )

    output_document.save(outfile)

여러 PDF에서 선택한 페이지 범위 병합

  1. 원본 PDF 문서를 로드합니다.
  2. 출력 문서를 생성합니다.
  3. 각 문서의 페이지 범위를 정의합니다.
  4. 첫 번째 문서의 페이지를 추가합니다.
  5. 두 번째 문서의 페이지를 추가합니다.
  6. 원하는 순서로 페이지를 결합합니다.
  7. 병합된 PDF를 저장합니다.
import sys
import aspose.pdf as ap
from os import path


def merge_selected_page_ranges(infile1, infile2, outfile):
    document1 = ap.Document(infile1)
    document2 = ap.Document(infile2)
    output_document = ap.Document()

    _append_page_range(document1, output_document, 1, 2)
    _append_page_range(document2, output_document, 2, 3)

    output_document.save(outfile)

특정 위치에 한 PDF를 다른 PDF에 삽입

  1. 베이스를 로드하고 문서를 삽입합니다.
  2. 출력 문서를 생성합니다.
  3. 기본 문서의 전체 페이지 수를 결정합니다.
  4. 삽입 색인의 유효성을 검사합니다.
  5. 삽입점 앞에 페이지를 추가합니다.
  6. 삽입 문서의 모든 페이지를 추가합니다.
  7. 기본 문서의 나머지 페이지를 추가합니다.
  8. 결과 PDF를 저장합니다.
import sys
import aspose.pdf as ap
from os import path


def merge_insert_document_at_position(infile1, infile2, insert_after_page, outfile):
    base_document = ap.Document(infile1)
    insert_document = ap.Document(infile2)
    output_document = ap.Document()

    base_total_pages = len(base_document.pages)
    insert_index = max(0, min(insert_after_page, base_total_pages))

    _append_page_range(base_document, output_document, 1, insert_index)
    _append_page_range(insert_document, output_document, 1, len(insert_document.pages))
    _append_page_range(
        base_document, output_document, insert_index + 1, base_total_pages
    )

    output_document.save(outfile)

페이지를 번갈아 가며 PDF 병합

이 예제에서는 Python용 Aspose.PDF 를 사용하여 두 PDF 문서의 페이지를 번갈아 병합하는 방법을 보여줍니다.

  1. 입력 PDF 문서를 로드합니다.
  2. 출력 문서를 생성합니다.
  3. 각 문서의 페이지 수를 구합니다.
  4. 최대 페이지 수를 계산합니다.
  5. 페이지 번호를 반복해서 살펴보세요.
  6. 페이지를 번갈아 추가합니다.
  7. 페이지 수가 같지 않은 경우 처리
  8. 병합된 PDF를 저장합니다.
import sys
import aspose.pdf as ap
from os import path


def merge_alternating_pages(infile1, infile2, outfile):
    document1 = ap.Document(infile1)
    document2 = ap.Document(infile2)
    output_document = ap.Document()

    document1_pages = len(document1.pages)
    document2_pages = len(document2.pages)
    max_pages = max(document1_pages, document2_pages)

    for page_number in range(1, max_pages + 1):
        if page_number <= document1_pages:
            output_document.pages.add(document1.pages[page_number])
        if page_number <= document2_pages:
            output_document.pages.add(document2.pages[page_number])

    output_document.save(outfile)

섹션 구분자 및 책갈피로 PDF 병합

Python용 Aspose.PDF 를 사용하여 여러 PDF 문서를 구조화된 섹션 및 탐색 북마크가 있는 단일 파일로 병합합니다.

  1. 출력 문서를 생성합니다.
  2. 입력 파일을 반복합니다.
  3. 소스 문서를 로드합니다.
  4. 구분선 페이지를 추가합니다.
  5. 섹션 북마크를 생성합니다.
  6. 소스 문서 페이지를 추가합니다.
  7. 첫 번째 콘텐츠 페이지를 추적합니다.
  8. 중첩된 콘텐츠 북마크를 추가합니다 (선택 사항).
  9. 모든 문서에 대해 이 작업을 반복합니다.
  10. 병합된 PDF를 저장합니다.
import sys
import aspose.pdf as ap
from os import path


def merge_with_section_separators_and_bookmarks(input_files, outfile):
    output_document = ap.Document()

    for section_index, input_file in enumerate(input_files, start=1):
        source_document = ap.Document(input_file)
        source_page_count = len(source_document.pages)

        separator_page = output_document.pages.add()
        separator_page.paragraphs.add(
            ap.text.TextFragment(
                f"Section {section_index}: {path.basename(input_file)}"
            )
        )

        section_bookmark = ap.OutlineItemCollection(output_document.outlines)
        section_bookmark.title = f"Section {section_index}"
        section_bookmark.action = ap.annotations.GoToAction(separator_page)
        output_document.outlines.append(section_bookmark)

        first_content_page_number = len(output_document.pages) + 1
        _append_page_range(source_document, output_document, 1, source_page_count)

        if source_page_count > 0 and first_content_page_number <= len(
            output_document.pages
        ):
            content_bookmark = ap.OutlineItemCollection(output_document.outlines)
            content_bookmark.title = f"Section {section_index} Content"
            content_bookmark.action = ap.annotations.GoToAction(
                output_document.pages[first_content_page_number]
            )
            section_bookmark.append(content_bookmark)

    output_document.save(outfile)

라이브 예제

Aspose.PDF 합병 프레젠테이션 병합 기능이 어떻게 작동하는지 조사할 수 있는 온라인 무료 웹 응용 프로그램입니다.

Aspose.PDF 합병

관련 문서 주제