问题描述
我想使用pypdf创建/修改pdf文档的标题.标题似乎是只读的.有没有办法访问该元数据读/写?
I'd like to create/modify the title of a pdf document using pypdf. It seems that the title is readonly. Is there a way to access this metadata r/w?
如果答案是肯定的,那么将感谢您提供一段代码.
If answer positive, a piece of code would be appreciated.
谢谢
推荐答案
您可以使用pyPDF(某种)来处理标题.我在reportlab-users列表上发现了该帖子:
You can manipulate the title with pyPDF (sort of). I came across this post on the reportlab-users listing:
http://two.pairlist.net/pipermail/reportlab-users/2009-November/009033.html
这不会让您编辑元数据 本身,但可以让您阅读或 更多pdf文件,然后将其吐回去 可能带有新的元数据.
This won't let you edit the metadata per se, but will let you read one or more pdf file(s) and spit them back out, possibly with new metadata.
以下是相关代码:
from pyPdf import PdfFileWriter, PdfFileReader
from pyPdf.generic import NameObject, createStringObject
OUTPUT = 'output.pdf'
INPUTS = ['test1.pdf', 'test2.pdf', 'test3.pdf']
# There is no interface through pyPDF with which to set this other then getting
# your hands dirty like so:
infoDict = output._info.getObject()
infoDict.update({
NameObject('/Title'): createStringObject(u'title'),
NameObject('/Author'): createStringObject(u'author'),
NameObject('/Subject'): createStringObject(u'subject'),
NameObject('/Creator'): createStringObject(u'a script')
})
inputs = [PdfFileReader(i) for i in INPUTS]
for input in inputs:
for page in range(input.getNumPages()):
output.addPage(input.getPage(page))
outputStream = file(OUTPUT, 'wb')
output.write(outputStream)
outputStream.close()
这篇关于用pypdf更改pdf文件的元数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!