pymupdf提取pdf表格及表格数据合并

pymupdf提取pdf表格非常快速,相比其他库是个更好的选择.

一个行列多的表格打印成pdf后会由于页宽分页原因变成多个表格,提取的多个表格需要合并为一个表格,再来处理数据.

下面代码中merge函数用于合并表格.addOneAxis0用于合并仅分页导致的多子表.

python 复制代码
def addOneAxis0(data,one):
    if len(data)==0:#first
        r=one
    else:
        r=data+one[1:]#remove first title row
    return r    

def merge(data):
    output=data[0]
    titles=data[0][0][1:]
    for i in range(1,len(data)):
        newtitle=data[i][0][1:]
        # print(titles)
        # print(newtitle)
        # input("here")
        if newtitle[0] in titles:#repeat
            for one in data[i][1:]:#add row
                output.append(one)
        else:#
            if newtitle[0] in output[0]:#add column without title
                thedata=data[i][1:]#remove title
                n=len(thedata)
                out=output[-n:]
                for j in range(len(out)):
                    out[j]+=thedata[j][1:]
                    pass
            else:#add column with title
                n=len(data[i])
                out=output[-n:]
                for j in range(len(out)):#add column
                    out[j]+=data[i][j][1:]
                    pass
    return output
def getDataMass(file_name):
    global doc,curvePage
    doc=fitz.open(file_name) # open document
    data=[]
    mass=[]
    tables=[]
    i=None
    for i in range(doc.page_count).__reversed__():
        page = doc[i]
        tabs=page.find_tables()
        if len(tabs.tables)==0:
            break
        else:
            tables.append(tabs[0].extract())
    tables.reverse()
    for one in tables:
        if one[0][0]=="溶液标签":
            data.append(one)#data.append(one)
        else:
            mass=addOneAxis0(mass,one)
    curvePage=i
    print(data)
    data=merge(data)
    print(data)
    return (data,mass)
相关推荐
百事牛科技5 小时前
PDF如何设置密码?3种方法保护文件安全
windows·pdf
mysusheng6 小时前
2025 批量下载微博内容/图片/视频,导出word和pdf,微博点赞/评论/转发等数据导出excel
pdf·word·excel
lijun_xiao20096 小时前
Python-PDF文件生成水印
pdf
zongxingfengyun6 小时前
图片转pdf接口
pdf
cpych6 小时前
如何从 PDF 中删除页面
pdf
开开心心就好13 小时前
无需函数:Excel数据筛选工具推荐
xml·网络·pdf·华为云·word·excel·音视频
拓端研究室1 天前
专题:2025构建全自动驾驶汽车生态系统:中国智能驾驶行业全景研究报告|附80+份报告PDF、数据仪表盘汇总下载
pdf·自动驾驶·汽车
南风微微吹1 天前
【管综】考研199管理类联考真题试卷及答案解析PDF电子版(2009-2025年)
考研·pdf
有过~2 天前
Total PDF Converter v6.5.0.356.0 电脑PDF多功能转换器
pdf
E_ICEBLUE2 天前
Python 教程:如何快速在 PDF 中添加水印(文字、图片)
开发语言·python·pdf