本文整理汇总了Python中pyPdf.PdfFileWriter.encrypt方法的典型用法代码示例。如果您正苦于以下问题:Python PdfFileWriter.encrypt方法的具体用法?Python PdfFileWriter.encrypt怎么用?Python PdfFileWriter.encrypt使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pyPdf.PdfFileWriter
的用法示例。
在下文中一共展示了PdfFileWriter.encrypt方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: merge_pdf
# 需要导入模块: from pyPdf import PdfFileWriter [as 别名]
# 或者: from pyPdf.PdfFileWriter import encrypt [as 别名]
def merge_pdf(new_filename, pdfs, encryp=False, user_pw="", owner_pw=None, lvl=128):
"""
Merges pdfs into one pdf called new_filename.
pdf: list of tuples (path=string, password=string)
"""
output = PdfFileWriter()
for path, pw in pdfs:
pdf = PdfFileReader(open(path, "rb"))
if pdf.isEncrypted:
decryption = pdf.decrypt(pw)
if decryption == 0:
raise PasswordError
for page_num in range(pdf.getNumPages()):
page = pdf.getPage(page_num)
output.addPage(page)
with file(new_filename, "wb") as outputStream:
outputStream = open(new_filename, "wb")
if encryp and lvl == 128:
output.encrypt(user_pw, owner_pw, True)
elif encryp:
output.encrypt(user_pw, owner_pw, False)
output.write(outputStream)
示例2: protect
# 需要导入模块: from pyPdf import PdfFileWriter [as 别名]
# 或者: from pyPdf.PdfFileWriter import encrypt [as 别名]
def protect(ifname, ofname, password):
with open(ifname, "rb") as ifile, open(ofname, "wb") as ofile:
reader = PdfFileReader(ifile)
writer = PdfFileWriter()
for i in range(reader.getNumPages()):
writer.addPage(reader.getPage(i))
writer.encrypt(password)
writer.write(ofile)
示例3: encrypt
# 需要导入模块: from pyPdf import PdfFileWriter [as 别名]
# 或者: from pyPdf.PdfFileWriter import encrypt [as 别名]
def encrypt(pdf_path, out_path, password, metadata=None):
if metadata == None:
title = _('Unknown')
author = _('Unknown')
else:
title = metadata.title
author = authors_to_string(metadata.authors)
out_pdf = PdfFileWriter(title=title, author=author)
pdf = PdfFileReader(open(os.path.abspath(pdf_path), 'rb'))
for page in pdf.pages:
out_pdf.addPage(page)
with open(out_path, 'wb') as out_file:
out_pdf.encrypt(str(password))
out_pdf.write(out_file)
示例4: export_to_file
# 需要导入模块: from pyPdf import PdfFileWriter [as 别名]
# 或者: from pyPdf.PdfFileWriter import encrypt [as 别名]
def export_to_file(self, file_out, only_selected=False):
"""Export to file"""
selection = self.iconview.get_selected_items()
pdf_output = PdfFileWriter()
pdf_input = []
for pdfdoc in self.pdfqueue:
pdfdoc_inp = PdfFileReader(file(pdfdoc.copyname, 'rb'))
if pdfdoc_inp.getIsEncrypted():
try: # Workaround for lp:#355479
stat = pdfdoc_inp.decrypt('')
except:
stat = 0
if (stat != 1):
errmsg = _('File %s is encrypted.\n'
'Support for encrypted files has not been implemented yet.\n'
'File export failed.') % pdfdoc.filename
raise Exception, errmsg
# FIXME
# else
# ask for password and decrypt file
pdf_input.append(pdfdoc_inp)
for row in self.model:
if only_selected and row.path not in selection:
continue
# add pages from input to output document
nfile = row[2]
npage = row[3]
current_page = copy(pdf_input[nfile - 1].getPage(npage - 1))
angle = row[6]
angle0 = current_page.get("/Rotate", 0)
crop = [row[7], row[8], row[9], row[10]]
if angle != 0:
current_page.rotateClockwise(angle)
if crop != [0., 0., 0., 0.]:
rotate_times = (((angle + angle0) % 360 + 45) / 90) % 4
crop_init = crop
if rotate_times != 0:
perm = [0, 2, 1, 3]
for it in range(rotate_times):
perm.append(perm.pop(0))
perm.insert(1, perm.pop(2))
crop = [crop_init[perm[side]] for side in range(4)]
# (x1, y1) = current_page.cropBox.lowerLeft
# (x2, y2) = current_page.cropBox.upperRight
(x1, y1) = [float(xy) for xy in current_page.mediaBox.lowerLeft]
(x2, y2) = [float(xy) for xy in current_page.mediaBox.upperRight]
x1_new = int(x1 + (x2 - x1) * crop[0])
x2_new = int(x2 - (x2 - x1) * crop[1])
y1_new = int(y1 + (y2 - y1) * crop[3])
y2_new = int(y2 - (y2 - y1) * crop[2])
# current_page.cropBox.lowerLeft = (x1_new, y1_new)
# current_page.cropBox.upperRight = (x2_new, y2_new)
current_page.mediaBox.lowerLeft = (x1_new, y1_new)
current_page.mediaBox.upperRight = (x2_new, y2_new)
pdf_output.addPage(current_page)
#encrypting the file
pdf_output.encrypt(self.getPassword()) #calling password function inside
resultPdf = open(file_out, 'wb')
pdf_output.write(resultPdf)