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_suye5 分钟前
C++11新特性探索:Lambda表达式与函数包装器的实用指南
开发语言·数据库·c++·人工智能·python·机器学习·华为
weixin_543662866 分钟前
BERT的中文问答系统36-1
人工智能·python·bert
weixin_431470868 分钟前
人名分类器(nlp)
人工智能·pytorch·python·深度学习·自然语言处理
学Linux的语莫10 分钟前
ansible变量
linux·运维·服务器·ansible
北京迅为12 分钟前
【北京迅为】iTOP-4412全能版使用手册-第十二章 Linux系统编程简介
linux·嵌入式硬件·4412开发板
努力更新中19 分钟前
Python浪漫之画一个音符♪
开发语言·python
清源妙木真菌20 分钟前
Linux:进程控制
linux
爱吃喵的鲤鱼22 分钟前
Linux——文件系统清尾、动静态库
linux·运维·服务器
泰山小张只吃荷园25 分钟前
期末Python复习-输入输出
java·前端·spring boot·python·spring cloud·docker·容器
凤枭香37 分钟前
Python Selenium介绍(二)
开发语言·爬虫·python·selenium