如何使用Python自动删除pdf文件中的页眉和页脚?

2024-04-24 13:55:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用PyPDF2和ipywidgets创建一个用户界面,让用户上传pdf文件并从中读取文本。问题是PyPDF2正在将页脚和页眉捕获到文本中。用户可以上传任何文件,因此删除这些文件的过程必须是自动化的,可以用于任何文件。是否有任何方法可以使用PyPDF2实现这一点

谢谢

代码:

def content_getter():
    #Reads from the ipywidget
    file = io.BytesIO(ui.children[0].children[1].data[0])

    pdfReader = PyPDF2.PdfFileReader(file)
    numPages = pdfReader.getNumPages()
    allLines = []
    for pageno in range(numPages):
        pdfReader = PyPDF2.PdfFileReader(file)
        pageObj = pdfReader.getPage(pageno)
        text = pageObj.extractText()
        li = text.splitlines()
        allLines = allLines + li
    final_output = ''.join(allLines)   
    return final_output,allLines

Tags: 文件text用户文本lifilefinalchildren