python实现pdf转换成word/txt纯文本文件

yipeiwu_com6年前Python基础

本文实例为大家分享了python实现pdf转word/txt,供大家参考,具体内容如下

依赖包:pdfminer3k

可以通过pip安装;也可以到官网下载,解压,进入文件夹,输入命令setup.py install安装软件。

源代码:

#!/usr/bin/python 
# -*- coding: utf-8 -*- 
 
import sys 
import importlib 
importlib.reload(sys) 
 
from pdfminer.pdfparser import PDFParser,PDFDocument 
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter 
from pdfminer.converter import PDFPageAggregator 
from pdfminer.layout import * 
from pdfminer.pdfinterp import PDFTextExtractionNotAllowed 
 
''''' 
解析pdf文件,获取文件中包含的各种对象 
''' 
 
# 解析pdf文件函数 
def parse(pdf_path): 
  fp = open(pdf_path, 'rb') # 以二进制读模式打开 
  # 用文件对象来创建一个pdf文档分析器 
  parser = PDFParser(fp) 
  # 创建一个PDF文档 
  doc = PDFDocument() 
  # 连接分析器 与文档对象 
  parser.set_document(doc) 
  doc.set_parser(parser) 
 
  # 提供初始化密码 
  # 如果没有密码 就创建一个空的字符串 
  doc.initialize() 
 
  # 检测文档是否提供txt转换,不提供就忽略 
  if not doc.is_extractable: 
    raise PDFTextExtractionNotAllowed 
  else: 
    # 创建PDf 资源管理器 来管理共享资源 
    rsrcmgr = PDFResourceManager() 
    # 创建一个PDF设备对象 
    laparams = LAParams() 
    device = PDFPageAggregator(rsrcmgr, laparams=laparams) 
    # 创建一个PDF解释器对象 
    interpreter = PDFPageInterpreter(rsrcmgr, device) 
 
    # 用来计数页面,图片,曲线,figure,水平文本框等对象的数量 
    num_page, num_image, num_curve, num_figure, num_TextBoxHorizontal = 0, 0, 0, 0, 0 
 
    # 循环遍历列表,每次处理一个page的内容 
    for page in doc.get_pages(): # doc.get_pages() 获取page列表 
      num_page += 1 # 页面增一 
      interpreter.process_page(page) 
      # 接受该页面的LTPage对象 
      layout = device.get_result() 
      for x in layout: 
        if isinstance(x,LTImage): # 图片对象 
          num_image += 1 
        if isinstance(x,LTCurve): # 曲线对象 
          num_curve += 1 
        if isinstance(x,LTFigure): # figure对象 
          num_figure += 1 
        if isinstance(x, LTTextBoxHorizontal): # 获取文本内容 
          num_TextBoxHorizontal += 1 # 水平文本框对象增一 
          # 保存文本内容 
          with open(r'test.doc', 'a',encoding='utf-8') as f:  #生成doc文件的文件名及路径 
            results = x.get_text() 
            f.write(results) 
            f.write('\n') 
    print('对象数量:\n','页面数:%s\n'%num_page,'图片数:%s\n'%num_image,'曲线数:%s\n'%num_curve,'水平文本框:%s\n' 
       %num_TextBoxHorizontal) 
 
 
if __name__ == '__main__': 
  pdf_path = r'test.pdf' #pdf文件路径及文件名 
  parse(pdf_path) 

此脚本只能将pdf文件转换成纯文本文件,没有任何格式。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python使用opencv实现马赛克效果示例

python使用opencv实现马赛克效果示例

本文实例讲述了python使用opencv实现马赛克效果。分享给大家供大家参考,具体如下: 最近要实现opencv视频打马赛克,在网上找了一下基本是C++的实现,好在原理一样,下面给出p...

pygame学习笔记(6):完成一个简单的游戏

学了这么长时间的Pygame,一直想写个游戏实战一下。看起来很简单的游戏,写其来怎么这么难。最初想写个俄罗斯方块,想了很长时间如何实现,想来想去,也没写出来,于是干脆下载别人的代码来读。...

Python实现数值积分方式

Python实现数值积分方式

原理: 利用复化梯形公式,复化Simpson公式,计算积分。 步骤: import math """测试函数""" def f(x,i): if i == 1: re...

Ubuntu18.04中Python2.7与Python3.6环境切换

Ubuntu18.04中Python2.7与Python3.6环境切换

本文为大家分享了Python2.7与Python3.6环境切换的具体方法,供大家参考,具体内容如下 系统支持为:Ubuntu18.04 系统默认安装:Python2.7 自己安装:Pyt...

python中关于时间和日期函数的常用计算总结(time和datatime)

1.获取当前时间的两种方法: 复制代码 代码如下:import datetime,timenow = time.strftime("%Y-%m-%d %H:%M:%S")print no...