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

相关推荐
cdut_suye2 分钟前
【Linux系统】探索进程等待与程序替换的奥秘
java·linux·运维·服务器·c++·人工智能·python
ValidationExpression5 分钟前
设计模式-工厂模式
python·设计模式
正经教主1 小时前
【基础】Linux 系统有多种安装工具比较
linux·运维·服务器
LingRannn6 小时前
【最新Python包管理工具UV的介绍和安装】
开发语言·python·uv
yuanjun04167 小时前
RTDETRv2 pytorch训练
人工智能·pytorch·python
杰克逊的日记7 小时前
什么是PyTorch
人工智能·pytorch·python
Y_Hungry8 小时前
Linux 怎么使用局域网内电脑的网络访问外部
linux·运维·服务器
后藤十八里8 小时前
Python格式化字符串的四种方法
开发语言·python·学习
10000hours8 小时前
【C语言编译】编译原理和详细过程
linux·c语言·笔记
小小不董9 小时前
Oracle OCP认证考试考点详解083系列06
linux·数据库·oracle·dba