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)
相关推荐
陌殇殇1 小时前
Java使用IText7动态生成带审批文本框的PDF文档
java·pdf
Ven%9 小时前
VsCode如何使用默认程序打开word Excel pdf等文件
ide·vscode·pdf·编辑器
软件工程小施同学10 小时前
计算机学报 2024年 区块链论文 录用汇总 附pdf下载
pdf·区块链
Sherlock Ma1 天前
PDFMathTranslate:基于LLM的PDF文档翻译及双语对照的工具【使用教程】
人工智能·pytorch·语言模型·pdf·大模型·机器翻译·deepseek
kadog1 天前
PubMed PDF下载 cloudpmc-viewer-pow逆向
前端·javascript·人工智能·爬虫·pdf
小墨宝1 天前
js 生成pdf 并上传文件
前端·javascript·pdf
热水养鲨鱼1 天前
Java实现HTML转PDF(deepSeekAi->html->pdf)
人工智能·pdf·html
开开心心_Every2 天前
体积小巧的 Word 转 PDF 批量工具
pdf
一眼青苔2 天前
切割PDF使用python,库PyPDF2
服务器·python·pdf
zyk_5202 天前
前端渲染pdf文件解决方案-pdf.js
前端·javascript·pdf