在 Python 中统计 PDF 工件
Contents
[
Hide
]
统计特定类型的工件
检查并计数 PDF 中的分页工件 Document 使用 Aspose.PDF for Python via .NET。分页工件包括水印、背景、页眉和页脚等元素,这些元素被应用于页面以进行布局和标识目的。通过过滤 Artifact 对象在 a Page 并按子类型分组(Artifact.ArtifactSubtype),开发人员可以快速分析文档的结构并验证特定元素的存在。
要计算特定类型工件的总数(例如,水印的总数),请使用以下代码。示例过滤页面的 Artifacts 集合 (一个 ArtifactCollection) 通过 Artifact.ArtifactType 然后计数子类型(Artifact.ArtifactSubtype).
from os import path
from collections import Counter
import sys
import aspose.pdf as ap
def count_pdf_artifacts(infile):
"""Count and display artifacts of different types on the first page."""
with ap.Document(infile) as document:
pagination_artifacts = [
artifact
for artifact in document.pages[1].artifacts
if artifact.type == ap.Artifact.ArtifactType.PAGINATION
]
subtypes = [artifact.subtype for artifact in pagination_artifacts]
counts = Counter(subtypes)
print(f"Watermarks: {counts.get(ap.Artifact.ArtifactSubtype.WATERMARK, 0)}")
print(f"Backgrounds: {counts.get(ap.Artifact.ArtifactSubtype.BACKGROUND, 0)}")
print(f"Headers: {counts.get(ap.Artifact.ArtifactSubtype.HEADER, 0)}")
print(f"Footers: {counts.get(ap.Artifact.ArtifactSubtype.FOOTER, 0)}")