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)
相关推荐
TextIn智能文档云平台3 小时前
如何快速提取PDF文档内容
pdf
小马爱打代码4 小时前
Spring AI:DeepSeek 整合 RAG 增强检索: 实现与 PDF 对话
人工智能·spring·pdf
南风微微吹4 小时前
英语六级作文历年真题及范文模版汇总PDF电子版(2015-2025年6月)
pdf·英语六级
verse_armour5 小时前
markdown插入文献引用并导出pdf
pdf·markdown·pandoc·latex
月屯7 小时前
pandoc安装与使用(html、makdown转docx、pdf)
前端·pdf·html·pandoc·转docx、pdf
星空椰1 天前
Java Excel转PDF
pdf·excel
Charlene Fung1 天前
如何使用 Markdown 生成带参考文献的 PDF
论文阅读·pdf·markdown
std860211 天前
微软发布 Edge 143 稳定版:PDF 注释支持撤销重做
microsoft·edge·pdf
shadowcz0071 天前
关于GEO的研究总结#使用 Notebooklm 来研究论文和整理报告#PDF分享
人工智能·pdf
czliutz2 天前
使用pdfplumber库处理pdf文件获取文本图片作者等信息
python·pdf