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)
相关推荐
南风微微吹11 小时前
计算机二级WPS office历年真题试卷及答案解析14套电子版PDF(2026年3月最新)
pdf·wps·计算机二级wps
SEO-狼术11 小时前
Convert HTML Tables to PDF in Python
开发语言·python·pdf
像风一样的男人@12 小时前
python --html转pdf/pdf分页优化
python·pdf·html
百事牛科技14 天前
保护文档安全:PDF限制功能详解与实操
windows·pdf
开开心心就好14 天前
安卓开源应用,超时提醒紧急人护独居安全
windows·决策树·计算机视觉·pdf·计算机外设·excel·动态规划
Felicia-侧听15 天前
如何统一PDF页面宽度?统一pdf宽度的2种方法
pdf·pdf宽度统一
开开心心_Every15 天前
音频格式互转工具,支持Mp3ApeWavFlac互转
linux·运维·服务器·typescript·edge·pdf·asp.net
2501_9307077815 天前
如何使用C#代码从 PDF 中提取表格并另存为Excel文件
pdf·excel
悟乙己15 天前
实施手册:如何自己构建财务PDF文件内容抽取引擎
pdf
xin_yao_xin15 天前
PDF 转 图片(python)
python·pdf