我想在一个pdf文件中替换一个单词,但当我尝试这样做时,它总是返回相同的pdf文件。这是我的代码块。当前我使用的是pypdf2,但如果有任何建议,我可以切换它。我的代码中缺少的部分是什么?
with open(file_path, 'rb') as file:
pdf_reader = PdfFileReader(file)
# Encrypt the word in the PDF content
encrypted_word = self.cipher.encrypt(word_to_encrypt_bytes)
encrypted_word_b64 = base64.b64encode(encrypted_word)
# Write the encrypted PDF content to a new PDF file
pdf_writer = PdfFileWriter()
for i in range(pdf_reader.getNumPages()):
page = pdf_reader.getPage(i)
page_content = page.extractText()
page_content_b = page_content.encode('utf-8')
page_content_b = page_content_b.replace(word_to_encrypt.encode(), encrypted_word_b64)
page_content = page_content_b.decode('utf-8')
pdf_writer.addPage(page)
output_path = os.path.join(file_dir, file_name_without_ext + '_encryptedm' + ext)
with open(output_path, 'wb') as output_file:
pdf_writer.write(output_file)
我想在我的pdf里放一个词。
1条答案
按热度按时间2q5ifsrm1#
看起来您只是替换了提取文本中的单词,而实际上并没有更新PDF页面内容。为此,您可以使用页面对象的setContentStreams方法用更新的内容替换内容流。
下面是一个更新的代码块,应该工作:
在这段更新后的代码中,我们首先将页面内容提取为文本,替换单词,然后将其转换回字节,然后使用getContents方法获取页面的现有内容流,并在q操作符上拆分它们(其标记新图形状态的开始),并将Q运算符附加到更新的内容流(因为第一图形状态不包括在提取的内容中)最后,我们使用page对象的setContentStreams方法设置更新的内容流,并将更新的页面添加到PDF编写器。