[英]Put Header, Footer and page number for pdf with pdfpages matplotlib
I created PDF
file of figures with pdfpages
in matplotlib
.我在
PDF
中创建了带有pdfpages
的matplotlib
数字文件。
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.