python pdf 转图片 转txt 信息抽取 表格提取


import os

import fitz
import json
# os.chdir("data")
# print(os.listdir())
from collections import namedtuple
TextUnit = namedtuple('TextUnit', ['x0', 'y0', 'x1', 'y1', 'span', 'page_no'])
base_path = os.path.dirname(os.path.abspath(__file__))

data_dir_path = os.path.join(base_path, "data")

os.chdir(data_dir_path)

tasks = {os.path.splitext(file)[0]:os.path.join(data_dir_path, file) for file in os.listdir()}

# 转 txt
def to_txt():
    tasks_len = len(tasks)
    for index, fname in enumerate(tasks):
        print("{}-{}".format(index, tasks_len))
        os.chdir(os.path.join(base_path, "txt2"))

        dir_name = str(index) + "-"+fname
        os.mkdir(dir_name)
        os.chdir(os.path.join(base_path, "txt2", dir_name))
        
        doc = fitz.open(tasks[fname])

        size_limit=-1
        nl_char='\n'
        sep_char=' '
        for page_no, page in enumerate(doc):
            # if page_no != 3:
            #     continue
            text_list = []

            page_num = str(page_no)
            page_len = len(page_num)
            page_num = page_num if page_len >= 3 else "0"+page_num if page_len == 2 else "00"+page_num

            new_line = True
            json_obj = json.loads(page.getText('json'))
            for block in json_obj.get('blocks', []):
                if new_line and text_list:
                    text_list.append("\n")
                for line in block.get('lines', []):
                    for span in line.get('spans', []):
                        text = span.get('text', '')
                        text_list.append(text)
                        x0, y0, x1, y1 = span['bbox']
                        new_line = text.endswith(' ')

                new_page = False
            out = open(fname + "-" + page_num + ".txt", "wb")          
            text = "".join(text_list).encode("utf8")  
            out.write(text)                       
            out.close()

# 转图片
def to_img():
    for index, fname in enumerate(tasks):
        os.chdir(os.path.join(base_path, "img"))

        dir_name = str(index) + "-"+fname
        os.mkdir(dir_name)
        os.chdir(os.path.join(base_path, "img", dir_name))
        
        doc = fitz.open(tasks[fname])
        for index, page in enumerate(doc):
            pix = page.getPixmap(alpha=False)
            pageimg_name = "page-%i.png" % page.number
            pix.writePNG(pageimg_name)

# 截取
def to_delete():
    for index, fname in enumerate(tasks):
        os.chdir(os.path.join(base_path, "to_pdf"))

        dir_name = str(index) + "-"+fname
        os.mkdir(dir_name)
        os.chdir(os.path.join(base_path, "to_pdf", dir_name))
        
        doc = fitz.open(tasks[fname])
        doc2 = fitz.open()
        doc2.insertPDF(doc, to_page = 9)  # first 10 pages
        # doc2.insertPDF(doc1, from_page = len(doc1) - 10) # last 10 pages
        doc2.save("first-and-last-10.pdf")
        # for index, page in enumerate(doc):
        #     pix = page.getPixmap(alpha=False)
        #     pageimg_name = "page-%i.png" % page.number
        #     pix.writePNG(pageimg_name)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

泡泡码客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值