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)
相关推荐
非凡ghost7 小时前
Xournal++(PDF文档注释工具) 中文绿色版
学习·pdf·生活·软件需求
MonkeyKing_sunyuhua7 小时前
使用ARQ做PDF OCR和 图片OCR的任务的方案
pdf·ocr
2501_930707789 小时前
如何使用C#代码在 PDF 中添加或删除附件
pdf
m5655bj9 小时前
如何使用 Python 调整 PDF 页面顺序?
python·pdf
YuanYWRS10 小时前
办公基础:实现PDF中表单不改变格式的情况下转成excel
pdf·excel
E_ICEBLUE1 天前
PDF vs PDF/A:区别、场景与常用转换方法(2025 全面解读)
python·pdf
TextIn智能文档云平台1 天前
从散乱资料到智能知识库:基于TextIn与Coze的RAG实战
人工智能·pdf·知识库·rag·coze·文档解析
m0_564914921 天前
图片怎么快速转为PDF?PDF怎么快速转为图片?
pdf
2501_930707781 天前
如何使用C#代码将多张图片整合为一个PDF文档
开发语言·pdf·c#
拓端研究室1 天前
赢战2025电商新趋势下的增长策略报告:平台格局、跨境趋势、大促消费|附600+份报告PDF、数据、可视化模板汇总下载
大数据·pdf