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)
相关推荐
其实秋天的枫2 天前
【2026年最新】驾考科目一考试题库2309道电子版pdf
经验分享·pdf
墨染天姬2 天前
【AI】如何基于cursor创建MCP索引pdf
人工智能·pdf
qq_429499572 天前
分享免费的PDF 翻译 原格式
pdf
来自外太空的鱼-张小张2 天前
jeecg预览pdf、jeecg无法预览pdf、jeecg自带预览pdf
pdf·状态模式
SEO-狼术2 天前
All-About-PDF provides DRM
pdf
珍朱(珠)奶茶2 天前
Spring Boot3整合FreeMark、itextpdf 5/7 实现pdf文件导出及注意问题
java·spring boot·后端·pdf·itextpdf
优化控制仿真模型2 天前
26年英语六级高频核心词汇1500个+历年真题PDF电子版
经验分享·pdf
xinixini3 天前
小初高全学科课程标准PDF
pdf
SQVIoMPLe3 天前
python-langchain框架(3-7-提取pdf中的图片 )
python·langchain·pdf
Highcharts.js3 天前
Highcharts客户端导出使用文档说明|图表导出模块讲解
前端·javascript·pdf·highcharts·图表导出