pdf格式转换为txt格式

pdf文档转换为txt文档

首先在python3虚拟环境中安装PyPDF2

Python 3.6.8 (default, Jun 20 2023, 11:53:23)

[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux

Type "help", "copyright", "credits" or "license" for more information.

>>> import sys

>>> sys.path

['', '/usr/lib64/python36.zip', '/usr/lib64/python3.6', '/usr/lib64/python3.6/lib-dynload', '/home/clusteruser/env3/lib64/python3.6/site-packages', '/home/clusteruser/env3/lib64/python3.6/site-packages/setuptools-58.0.4-py3.6.egg', '/home/clusteruser/env3/lib64/python3.6/site-packages/selenium-3.141.0-py3.6.egg', '/home/clusteruser/env3/lib64/python3.6/site-packages/urllib3-1.26.6-py3.6.egg', '/home/clusteruser/env3/lib/python3.6/site-packages', '/home/clusteruser/env3/lib/python3.6/site-packages/setuptools-58.0.4-py3.6.egg', '/home/clusteruser/env3/lib/python3.6/site-packages/selenium-3.141.0-py3.6.egg', '/home/clusteruser/env3/lib/python3.6/site-packages/urllib3-1.26.6-py3.6.egg']

>>> quit();

(env3) [clusteruser@node0xc7 pdf-txt]$ pip3 install --target='/home/clusteruser/env3/lib64/python3.6/site-packages' PyPDF2

Collecting PyPDF2

Downloading pypdf2-3.0.1-py3-none-any.whl (232 kB)

|████████████████████████████████| 232 kB 407 kB/s

Collecting typing_extensions>=3.10.0.0

Downloading typing_extensions-4.1.1-py3-none-any.whl (26 kB)

Collecting dataclasses

Downloading dataclasses-0.8-py3-none-any.whl (19 kB)

Installing collected packages: typing-extensions, dataclasses, PyPDF2

Successfully installed PyPDF2-3.0.1 dataclasses-0.8 typing-extensions-4.1.1

***************************************************************************************

完成代码

(env3) [clusteruser@node0xc7 pdf-txt]$ cat pdf-text.py

import PyPDF2

def pdf_to_text(pdf_path, txt_path):

with open(pdf_path, 'rb') as pdf_file:

reader = PyPDF2.PdfReader(pdf_file)

text = ''

for page_number in range(len(reader.pages)):

text += reader.pages[page_number].extract_text()

with open(txt_path, 'w', encoding='utf-8') as txt_file:

txt_file.write(text)

调用函数进行转换

pdf_to_text('input.pdf', 'output.txt')

执行代码

python3 pdf-text.py

相关推荐
bst@微胖子34 分钟前
Python高级语法之selenium
开发语言·python·selenium
查理零世2 小时前
【蓝桥杯集训·每日一题2025】 AcWing 6118. 蛋糕游戏 python
python·算法·蓝桥杯
魔尔助理顾问3 小时前
一个简洁高效的Flask用户管理示例
后端·python·flask
java1234_小锋3 小时前
一周学会Flask3 Python Web开发-request请求对象与url传参
开发语言·python·flask·flask3
诚信爱国敬业友善6 小时前
常见排序方法的总结归类
开发语言·python·算法
IT利刃出鞘7 小时前
禁止WPS强制打开PDF文件
pdf
架构默片7 小时前
【JAVA工程师从0开始学AI】,第五步:Python类的“七十二变“——当Java的铠甲遇见Python的液态金属
java·开发语言·python
IT_张三8 小时前
Ubuntu Linux运维实战指南4_文件系统基础知识
linux·运维·ubuntu
小哥山水之间8 小时前
在 Python 中操作 Excel 文件
开发语言·python·excel
wangjun51598 小时前
linux redis ipv6、ipv4 只接收本地访问、接收本地和远程访问
linux·运维·服务器