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)
相关推荐
机器学习算法与Python实战6 小时前
PDF 文件翻译,我有4个方案推荐
pdf
manjianghong861 天前
如何将一本书PDF扫描件转word 并打印(免费工具)
pdf·word·pdf处理工具
zhangfeng11331 天前
大语言模型llm学习路线电子书 PDF、开源项目、数据集、视频课程、面试题、工具镜像汇总成一张「一键下载清单」
学习·语言模型·pdf
manjianghong861 天前
PDF扫描件图片太大如何批量裁剪(免费工具)
pdf·pdf免费工具·pdf文件处理
YJlio1 天前
杨利杰YJlio|博客导航目录(专栏总览 + 推荐阅读路线)
开发语言·python·pdf
꧁༺℘₨风、凌๓༻꧂1 天前
C# WPF 项目中集成 Pdf查看器
pdf·c#·wpf
liliangcsdn2 天前
常用pdf解析提取工具的分析和示例
pdf
有趣灵魂2 天前
Java-Spingboot根据HTML模板和动态数据生成PDF文件
java·pdf·html
mfxcyh2 天前
使用html2canvas和jsPDF导出pdf文件、把pdf文件传给后端
pdf
ComPDFKit2 天前
从爱泼斯坦案文件泄露,看“涂黑≠删除”的 PDF 脱敏陷阱
pdf·脱敏·pdf redaction·标记密文·涂黑