【问题标题】:Put Header, Footer and page number for pdf with pdfpages matplotlib使用 pdfpages matplotlib 为 pdf 放置页眉、页脚和页码
【发布时间】:2023-01-12 19:24:10
【问题描述】:

我在matplotlib中用pdfpages创建了PDF图形文件。

现在我想添加页眉(包含文本和行)、页脚(包含文本和行)和页码。我怎样才能做到这一点?

【问题讨论】:

    标签: python matplotlib pdfpages


    【解决方案1】:

    使用 PyMuPDF:

    确定页眉和页脚矩形坐标,然后确定每个矩形的文本以及常量和可变部分。

    例子:

    页脚:一行,矩形底部距页面底部 0.5 英寸(36 点),字体大小为 11 磅,Helvetica 字体,文本居中“第 n 页,共 m 页”。

    页眉:一行,矩形顶部低于页面顶部 36 点,矩形高度 20 点,字体 Helvetica 粗体,文本“我的 Matplotlib 文件”居中。 11 号字体大小,颜色为蓝色。

    import fitz
    doc = fitz.open("matplotlib.pdf")
    numpages = doc.page_count  # number of pages
    footer_text = "Page %i of %i"
    header_text = "My Matplotlib File"
    blue = fitz.pdfcolor["blue"]
    
    for page in doc:
        prect = page.rect
        header_rect = fitz.Rect(0, 36, prect.width, 56)  # height 20 points
        page.insert_textbox(header_rect, header_text,
                            fontname="hebo", color=blue,
                            align=fitz.TEXT_ALIGN_CENTER)
    
        ftext = footer_text % (page.number + 1, numpages)
        y1 = prect.height - 36  # bottom of footer rect
        y0 = y1 - 20  # top of footer rect
        footer_rect = fitz.Rect(0, y0, prect.width, y1)  # rect has full page width
        page.insert_textbox(footer_rect, text, align=fitz.TEXT_ALIGN_CENTER)
    
    doc.save("matplotlib-numbered.pdf")
    

    【讨论】:

      猜你喜欢
      • 2013-09-30
      • 1970-01-01
      • 1970-01-01
      • 2013-05-16
      • 2014-06-10
      • 2015-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多