在 Python 中统计 PDF 工件
Contents
[
Hide
]
统计 PDF 页面上的分页工件,以检查水印、背景、页眉和页脚。
按子类型统计分页工件
按 PAGINATION 类型筛选第一页的 artifacts 集合,然后按子类型统计工件数量。没有相应子类型的工件时,示例输出 0。
from collections import Counter
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)}")