文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

python pdf

2023-01-31 03:25

关注
# 从pdf中读取文本
# 写pdf
# 加密解密pdf
# 和平pdf,加水印
# pip install PyPDF2
%cd D:\python全站\office
import PyPDF2
D:\python全站\office
pdf_obj = open('coop.pdf', 'rb')
pdf = PyPDF2.PdfFileReader(pdf_obj)
pdf.numPages
3
page = pdf.getPage(0)
page.extractText()  # 提取文件
'\n\n \n \n1\\\n1\nN¥\n \nde8ug word\n \nde8ug word\n \nde8ug word\n \nde8ug word\n \n\n\n \n \n\n \nde8ug word\n \nde8ug word\n \nde8ug word\n \nde8ug word\n \n \n\n\n \n \n\n \nde8ug word\n \nde8ug word\n \nde8ug word\n \nde8ug word\n \n \n'
# 提取中文 pip install pdfminer3k  #支持中文
from pdfminer.pdfinterp import PDFResourceManager, process_pdf # 资源管理
from pdfminer.converter import TextConverter  # 文本转换
from pdfminer.layout import LAParams #布局
from io import StringIO  # 生成临时文件

def convert_pdf(path):
    rsrcmgr = PDFResourceManager()
    retstr = StringIO()
    laparams = LAParams()
    device = TextConverter(rsrcmgr, retstr, laparams = laparams)
    fp = open(path, 'rb')
    process_pdf(rsrcmgr, device, fp)
    fp.close()
    device.close()
    out = retstr.getvalue()
    retstr.close()
    return out
s = convert_pdf('coop.pdf')
# print(s)
# convert_pdf('coop.pdf')
s.split('\n\x0c')
['测试语句 \n\n第 1 页 \n\nde8ug word \n\nde8ug word \n\nde8ug word \n\nde8ug word \n\n测试语句 \n\n第一页 \n\nde8ug word \n\nde8ug word \n\nde8ug word \n\nde8ug word \n\n测试语句 \n\n第一页 \n\nde8ug word \n\nde8ug word \n\nde8ug word \n\nde8ug word \n\n \n \n \n \n ',
 '测试语句 \n\n第 2 页 \n\nde8ug word \n\nde8ug word \n\nde8ug word \n\nde8ug word \n\nde8ug word \n\nde8ug word \n\nde8ug word \n\nde8ug word \n\nde8ug word \n\nde8ug word \n\nde8ug word \n\nde8ug word \n\nde8ug word \n\nde8ug word \n\nde8ug word \n\nde8ug word \n\nde8ug word \n\nde8ug word \n\nde8ug word \n\n \n \n \n \n ',
 'de8ug word \n\n测试语句 \n\n第 3 页 \n\nde8ug word \n\nde8ug word \n\nde8ug word \n\nde8ug word \n\n \n \n ',
 '']
# 写pdf,从上文打开的pdf找出第二页,新鞋一个pdf
pdf_writer = PyPDF2.PdfFileWriter()
page = pdf.getPage(1)
pdf_writer.addPage(page)
with open('coop-1.pdf', 'wb') as f:
    pdf_writer.write(f)
pdf_obj.close()
# 加密pdf
with open('coop.pdf', 'rb') as f_in:
    pdf = PyPDF2.PdfFileReader(f_in)
    pdf_writer = PyPDF2.PdfFileWriter()
    for page_num in range(pdf.numPages):
        pdf_writer.addPage(pdf.getPage(page_num))
    pdf_writer.encrypt('hicoop')
    with open('coop-s.pdf', 'wb') as f_out:
        pdf_writer.write(f_out)
# 解密
with open('coop-s.pdf', 'rb') as f_in:
    pdf = PyPDF2.PdfFileReader(f_in)
    print(pdf.isEncrypted)
    pdf.decrypt('hicoop')
    pdf.getPage(0) #取到解密后的数据才能正常操作
True
# 合并多个pdf,加水印
with open('coop.pdf', 'rb') as f_in:
    with open('coop-watermarked.pdf', 'rb') as f_w:
        pdf = PyPDF2.PdfFileReader(f_in)
        pdf_w = PyPDF2.PdfFileReader(f_w)

        pdf_write = PyPDF2.PdfFileWriter()
        for page_num in range(pdf.numPages):
            page = pdf.getPage(page_num)
            page.mergePage(pdf_w.getPage(0))
            pdf_write.addPage(page)
        with open('coop-watermarked.pdf', 'wb') as f_out:
            pdf_write.write(f_out)
---------------------------------------------------------------------------

OSError                                   Traceback (most recent call last)

<ipython-input-39-b87325251ec9> in <module>()
      3     with open('coop-watermarked.pdf', 'rb') as f_w:
      4         pdf = PyPDF2.PdfFileReader(f_in)
----> 5         pdf_w = PyPDF2.PdfFileReader(f_w)
      6 
      7         pdf_write = PyPDF2.PdfFileWriter()

c:\users\coop\miniconda3\envs\coop\lib\site-packages\PyPDF2\pdf.py in __init__(self, stream, strict, warndest, overwriteWarnings)
   1082             stream = BytesIO(b_(fileobj.read()))
   1083             fileobj.close()
-> 1084         self.read(stream)
   1085         self.stream = stream
   1086 

c:\users\coop\miniconda3\envs\coop\lib\site-packages\PyPDF2\pdf.py in read(self, stream)
   1687         if debug: print(">>read", stream)
   1688         # start at the end:
-> 1689         stream.seek(-1, 2)
   1690         if not stream.tell():
   1691             raise utils.PdfReadError('Cannot read an empty file')

OSError: [Errno 22] Invalid argument
阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯