利用python将图片版PDF转文字版PDF

本文介绍如何使用Python结合pypdf2、PythonMagick、百度文字识别服务和pdfkit,将无法复制的图片版PDF转换为可编辑的文字版PDF。
摘要由CSDN通过智能技术生成

图片版PDF无法复制,转化成文字版的PDF后使用更方便.
我们需要用到python3.6,pypdf2,ghostscript,PythonMagick,百度文字识别服务和pdfkit.

安装

安装python3.6 略
安装ghostscript
https://ghostscript.com/download/gsdnld.html
安装wkhtmltopdf
https://wkhtmltopdf.org/downloads.html
pip安装PyPDF2,ghostscript,baidu-aip,pdfkit

pip install PyPDF2
pip install ghostscript
pip install baidu-aip
pip install pdfkit

pip安装PythonMagick
https://www.lfd.uci.edu/~gohlke/pythonlibs/

cd 下载目录
pip install PythonMagick‑0.9.13‑cp36‑cp36m‑win_amd64.whl

pypdf2用于拆分和合并PDF

示例代码如下:

#导入PdfFileReader和PdfFileWriter
from PyPDF2 import PdfFileReader, PdfFileWriter
#获取一个pdf对象
pdf_input = PdfFileReader(open(r'pdf路径', 'rb'))
#获取pdf页数
page_count = pdf_input.getNumPages()
#获取pdf第四页的内容
page = pdf_input.getPage(3)
page['/Contents']
#获取一个pdfWriter对象
pdf_output = PdfFileWriter()
# 将一个 PageObject 加入到 PdfFileWriter 中
pdf_output.addPage(page)
#把新pdf保存
pdf_output.write(open(r'新pdf路径','wb'))

PythonMagick用于将单页PDF转化为jpg

百度云-文字识别-python SDK

每天有500次免费的识别
示例代码如下:

  • 6
    点赞
  • 57
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值