我的知识记录

Python批量为PDF添加公司Logo页眉:每页统一抬头

用Python的PyMuPDF批量给多个PDF每页顶部插入公司Logo和公司名称页眉,同时加页脚页码,适合对外报价单、合同、方案统一品牌形象。

场景痛点

销售发出去的报价单、合同、方案,来自不同人手里,有的有公司抬头有的没有,有的是旧版Logo,对外形象不统一。手动在Acrobat里每页加Logo和公司名,几十页上百份文件做一遍就下班了。用Python循环每个PDF每页,统一在顶部贴Logo、写公司名、底部加页码,一次跑完。

用到的库

pip install pymupdf

完整代码

# -*- coding: utf-8 -*-
"""
批量给 PDF 每页加公司 Logo 页眉 + 公司名称 + 页脚页码。
"""
import fitz  # PyMuPDF
from pathlib import Path


HEADER_TEXT = "稳驰供应链 · 内部报价单"
HEADER_COLOR = (0.2, 0.3, 0.6)   # 深蓝色
LINE_COLOR = (0.8, 0.8, 0.8)     # 灰色分隔线


def add_header_footer(input_pdf: str,
output_pdf: str,
logo_path: str = None):
doc = fitz.open(input_pdf)
total = doc.page_count

for page_index, page in enumerate(doc, start=1):
rect = page.rect
margin = 2 * 28.35   # 约 2cm

# 1) 左上角贴 Logo(如果提供了)
if logo_path and Path(logo_path).exists():
logo_height = 28
# 按图片原始比例算宽度
with fitz.open(logo_path) as ldoc:
lpage = ldoc[0]
ratio = lpage.rect.width / lpage.rect.height
logo_width = logo_height * ratio
logo_rect = fitz.Rect(
margin, margin - 22,
margin + logo_width, margin + logo_height - 22,
)
page.insert_image(logo_rect, filename=logo_path, overlay=True)

# 2) 页眉文字:Logo 右边写公司名
text_x = margin + (40 if logo_path else 0)
page.insert_text(
fitz.Point(text_x, margin),
HEADER_TEXT,
fontsize=10,
fontname="china-s",
color=HEADER_COLOR,
)

# 3) 页眉下方画一条灰色分隔线
line_y = margin + 8
page.draw_line(
fitz.Point(margin, line_y),
fitz.Point(rect.width - margin, line_y),
color=LINE_COLOR,
width=0.5,
)

# 4) 页脚:居中页码
footer_text = f"第 {page_index} 页 / 共 {total} 页"
text_width = fitz.get_text_length(footer_text, fontname="china-s", fontsize=9)
page.insert_text(
fitz.Point((rect.width - text_width) / 2, rect.height - margin),
footer_text,
fontsize=9,
fontname="china-s",
color=(0.4, 0.4, 0.4),
)

doc.save(output_pdf, garbage=3, deflate=True)
doc.close()
print(f"完成:{output_pdf}")


def batch_add_header(input_dir: str, output_dir: str, logo_path: str = None):
src, dst = Path(input_dir), Path(output_dir)
dst.mkdir(parents=True, exist_ok=True)

for pdf_file in src.glob("*.pdf"):
out = dst / pdf_file.name
try:
add_header_footer(str(pdf_file), str(out), logo_path)
except Exception as e:
print(f"失败:{pdf_file.name} -> {e}")


if __name__ == "__main__":
batch_add_header(
input_dir="originals",
output_dir="branded",
logo_path="company_logo.png",
)

代码讲解

  • 页眉区域统一在距顶部2cm位置:左边贴Logo(可选),右边紧跟公司名文字,下面一条灰色细线把页眉和正文分开。
  • fitz.get_text_length(...) 算出页码文字的宽度,然后居中放在页面底部,这是PDF里做居中文字的标准做法。
  • page.draw_line(p1, p2, color=..., width=...) 画分隔线,0.5磅灰色不抢眼。
  • 页码"第X页/共Y页"用enumerate(doc, start=1)doc.page_count自动算,不用手动数。
  • 批量函数遍历整个文件夹,输出到新目录,原文件不动;单个文件失败不影响其他。

运行结果

把PDF放进originals/、Logo命名company_logo.png放同级,跑完branded/里每个PDF每页左上角有Logo+公司名"稳驰供应链·内部报价单",页眉下面有一条灰线,底部居中页码。整体看起来像正规公司对外文档。

注意事项

  • 页眉会盖住原有内容,正文最顶部要留出至少1.5cm空白,否则会压到原文。如果原文已经很满,把页眉文字改成更小字号或放到页边距外。
  • Logo用透明PNG效果最好,白底图会有一块方块。
  • china-s内置字体不支持英文加粗,英文公司名想用粗体,换fontfile指定一个粗体ttf。
  • 想同时加联系方式、网址,在页眉文字里多拼几个字符串,或者在页脚右侧写一行小字。

Python批量为PDF添加公司Logo页眉:每页统一抬头

标签:

更新时间:2026-09-14 21:41:10

上一篇:Python批量打印PDF:自动发送到打印机

下一篇:Python给PDF添加文字水印:PyMuPDF批量防泄密