我有一个PDF文件,但我必须把它自己的形象。这是一个官方文件,我必须在整个页面上应用一个带有文本“example”的图像。如何在Python中将图像添加到PDF文件?
有什么办法可以解决这个问题在python中?
(该文档中的文本是曲线)
我有一个PDF文件,但我必须把它自己的形象。这是一个官方文件,我必须在整个页面上应用一个带有文本“example”的图像。如何在Python中将图像添加到PDF文件?
有什么办法可以解决这个问题在python中?
(该文档中的文本是曲线)
考查PyPDF。你可以使用类似下面的代码应用覆盖:
page = PdfFileReader(file("document.pdf", "rb")).getPage(0)
overlay = PdfFileReader(file("overlay.pdf", "rb")).getPage(0)
page.mergePage(overlay)
把你想要的任何覆盖,包括“示例”,为overlay.pdf
。 个人而言,我更喜欢PDFTK,虽然不是严格的Python,但可以从os.system(command)
的脚本中调用。
PyPDF有什么好运气?如果我的答案有效,请考虑接受。 –
如果您来自Google,PyPDF已被替换为PyPDF2。语法有所改变。
import PyPDF2 as pypdf
with open("original.pdf", "rb") as inFile, open("overlay.pdf", "rb") as overlay:
original = pypdf.PdfFileReader(inFile)
background = original.getPage(0)
foreground = pypdf.PdfFileReader(overlay).getPage(0)
# merge the first two pages
background.mergePage(foreground)
# add all pages to a writer
writer = pypdf.PdfFileWriter()
for i in range(original.getNumPages()):
page = original.getPage(i)
writer.addPage(page)
# write everything in the writer to a file
with open("modified.pdf", "wb") as outFile:
writer.write(outFile)
http://whathaveyoutried.com/ – Buggabill
我以前遇到过大约一个月了类似的问题,但不能很容易地找到一个Python的解决方案。现在写一个答案。 –