1 Star 4 Fork 3

hidadeng / pdfdocx

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

最近运行课件代码,发现pdf文件读取部分的函数失效。这里找到读取pdf文件的可运行代码,为了方便后续学习使用,我已将pdf和docx读取方法封装成pdfdocx包。

pdfdocx

只有简单的两个读取函数

  • read_pdf(file)
  • read_docx(file)

file为文件路径,函数运行后返回file文件内的文本数据。


安装

pip install pdfdocx

使用

读取pdf文件

from pdfdocx import read_pdf
p_text = read_pdf('test/data.pdf')
print(p_text)

Run

这是来⾃pdf⽂件内的内容
from pdfdocx import read_docx
d_text = read_pdf('test/data.docx')
print(d_text)

Run

这是来⾃docx⽂件内的内容

拆开pdfdocx

希望大家能安装好,如果安装或者使用失败,可以使用下面的代码作为备选方法。虽然繁琐,能用就好。

读取pdf

from io import StringIO
from pdfminer.converter import TextConverter
from pdfminer.layout import LAParams
from pdfminer.pdfdocument import PDFDocument
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.pdfpage import PDFPage
from pdfminer.pdfparser import PDFParser
import re


def read_pdf(file):
    """
    读取pdf文件,并返回其中的文本内容
    :param file: pdf文件路径
    :return: docx中的文本内容
    """
    output_string = StringIO()
    with open(file, 'rb') as in_file:
        parser = PDFParser(in_file)
        doc = PDFDocument(parser)
        rsrcmgr = PDFResourceManager()
        device = TextConverter(rsrcmgr, output_string, laparams=LAParams())
        interpreter = PDFPageInterpreter(rsrcmgr, device)
        for page in PDFPage.create_pages(doc):
            interpreter.process_page(page)
    text = output_string.getvalue()
    return text

  

读取docx

import docx
  
def read_docx(file):
    """
    读取docx文件,并返回其中的文本内容
    :param file: docx文件路径
    :return: docx中的文本内容
    """
    text = ''
    doc = docx.Document(file)
    for para in doc.paragraphs:
        text += para.text
    return text

如果

如果您是经管人文社科专业背景,编程小白,面临海量文本数据采集和处理分析艰巨任务,可以参看《python网络爬虫与文本数据分析》视频课。作为文科生,一样也是从两眼一抹黑开始,这门课程是用五年时间凝缩出来的。自认为讲的很通俗易懂o( ̄︶ ̄)o,

  • python入门
  • 网络爬虫
  • 数据读取
  • 文本分析入门
  • 机器学习与文本分析
  • 文本分析在经管研究中的应用

感兴趣的童鞋不妨 戳一下《python网络爬虫与文本数据分析》进来看看~


更多


MIT License Copyright (c) 2020 thunderhit Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

读取pdf、docx文件,返回文件内的文本数据。 展开 收起
Python
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Python
1
https://gitee.com/hidadeng/pdfdocx.git
git@gitee.com:hidadeng/pdfdocx.git
hidadeng
pdfdocx
pdfdocx
master

搜索帮助