pdfminer的TextConverter得到文件字符无空格解决方法

本文介绍了一种使用PDFMiner库将PDF文件转换为文本的方法,包括初始化线程、资源管理、布局参数设置及文本输出流程。通过实例演示了如何实现PDF到文本的高效转换,特别强调了字符间空格的保留对于保持原文格式的重要性。
from pdfminer.pdfinterp import PDFResourceManager, process_pdf
from pdfminer.converter import TextConverter
from pdfminer.layout import LAParams
import threading, os

class PdfThread(threading.Thread):
    def __init__(self, in_queue, doc_txt_dir):
        threading.Thread.__init__(self)
        self.in_queue = in_queue
        self.doc_txt_dir = doc_txt_dir

    def run(self):
        while True:
            try:
                codec = 'utf-8'
                in_fname = self.in_queue.get()
                rsrc = PDFResourceManager(caching = True)
                base_name = os.path.basename(in_fname)
                out_file = os.path.join(self.doc_txt_dir, base_name[0:base_name.rfind(".")] + ".txt")
                outfp = file(out_file, 'w')
                laparams = LAParams() #加上此参数可保留原pdf中的字符间空格
                device = TextConverter(rsrc, outfp, codec=codec, laparams=laparams)
                fp = file(in_fname, 'rb')
                caching = True
                pagenos = set()
                process_pdf(rsrc, device, fp, pagenos, maxpages=0, password='',caching=caching, check_extractable=True)
                fp.close()
                device.close()
                outfp.close()
                print "have convert pdf file %s to file %s" %(in_fname, out_file)
            finally:
                self.in_queue.task_done()
#TagExtractor

 

转载于:https://www.cnblogs.com/springbarley/p/3347538.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值