在 Python 中提取 PDF 页面
Contents
[
Hide
]
从 PDF 中提取单个页面
从 PDF 文档中提取特定页面并将其保存为新文件。使用 Aspose.PDF 库,脚本将所需页面复制到一个新的 PDF 中,保持原始文档不变。这对于拆分 PDF 或提取重要页面进行分发非常有用。
- 使用以下方式加载源 PDF
DocumentAPI (ap.Document()). - 创建一个新
Document用于保存提取的页面。 - 添加所需的
Page从源文档到新 PDF,使用目标文档的PageCollection(dst_document.pages.add(...)).- 在此示例中,提取第 2 页(基于 1 的索引)。
- 保存新的
Document将提取的页面保存到指定的输出文件中。
import aspose.pdf as ap
def extract_page(input_file_name: str, output_file_name: str) -> None:
src_document = ap.Document(input_file_name)
dst_document = ap.Document()
dst_document.pages.add(src_document.pages[2])
dst_document.save(output_file_name)
提取 PDF 的多个页面
从 PDF 文档中提取多个特定页面并将其保存到新文件中。使用 Aspose.PDF 库,所选页面会被复制到新的 PDF 中,同时保持原始文档不变。这对于创建仅包含较大文档中相关部分的更小 PDF 非常有用。
- 使用以下方式加载源 PDF
DocumentAPI (ap.Document()). - 创建一个新
Document用于保存提取的页面。 - 选择要提取的页面(在本例中,使用基于1的索引选择第2页和第3页)。
- 添加每个已选择的
Page从源文档到新PDF,使用其PageCollection. - 保存新的
Document将提取的页面写入指定的输出文件。
import aspose.pdf as ap
def extract_multiple_pages(input_file_name: str, output_file_name: str) -> None:
document = ap.Document(input_file_name)
pages = [2, 3]
another_document = ap.Document()
for page_index in pages:
another_document.pages.add(document.pages[page_index])
another_document.save(output_file_name)