简体   繁体   English

将 Header、pdf 的页脚和页码放入 pdf 页面 matplotlib

[英]Put Header, Footer and page number for pdf with pdfpages matplotlib

I created PDF file of figures with pdfpages in matplotlib .我在PDF中创建了带有pdfpagesmatplotlib数字文件。

Now I want to add headers (contains text and line), footer (contains text and line) and page number.现在我想添加页眉(包含文本和行)、页脚(包含文本和行)和页码。 How can I do that?我怎样才能做到这一点?

Use PyMuPDF:使用 PyMuPDF:

Decide the header and footer rectangle coordinates, then the text of each with the constant and variable parts.确定 header 和页脚矩形坐标,然后确定每个文本的常量和可变部分。

Example:例子:

Footer: One line, bottom of rectangle 0.5 inches (36 points) above bottom of page, 11 points font size, font Helvetica, text centered "Page n of m".页脚:一行,矩形底部距页面底部 0.5 英寸(36 点),字体大小为 11 磅,Helvetica 字体,文本居中“第 n 页,共 m 页”。

Header: One line, rectangle top 36 points below top of page, 20 points rect height, font Helvetica bold, text "My Matplotlib File" centered. Header:一行,矩形顶部低于页面顶部 36 点,矩形高度 20 点,字体 Helvetica 粗体,文本“我的 Matplotlib 文件”居中。 11 points font size, color blue. 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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何阻止从matplotlib PdfPages函数在PDF输出中创建额外页面 - How to stop extra page being created in PDF output from matplotlib PdfPages function 如何在 matplotlib 中使用 pdf 页面 plot 每页 4 个数字? - How to plot 4 figures per page with pdfpages in matplotlib? matplotlib使用pdfpages savefig dpi设置pdf光栅分辨率 - matplotlib setting pdf raster resolution using pdfpages savefig dpi 在 PdfPages 页面 Python 内的 matplotlib 图中添加超链接 - Add a hyperlink in a matplotlib plot inside a PdfPages page Python 使用PdfPages添加文本 - matplotlib - Add text with PdfPages - matplotlib 有什么方法可以提取PDF文档的页眉,页脚和标题页? - Is there any way to extract header and footer and title page of a PDF document? Anaconda:matplotlib PdfPages导入错误 - Anaconda: matplotlib PdfPages import error 使用 backend_pdf PdfPages 保存 matplotib 表时无法在 PDF 页面上居中 - Cannot center table on PDF-page when saving a matplotib table using backend_pdf PdfPages 在带有 PdfPages 的 matplotlib 中,如何将绘图区域设置为仅使用整页的上半部分? - In matplotlib with PdfPages, how do I set the plotting area to only use the top half of a full page? 在使用 PdfPages 生成的多页 pdf 中插入目录的简单方法 - A simple way to insert a table of contents in a multiple page pdf generated using PdfPages
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM