用Python编辑现有PDF页面

joh*_*ohn 6 python pdf

我有一个PDF文件,已从其中删除了一些页面。我想更正(修复)新的pdf页码。有什么方法/库可以在不将pdf转换为另一种格式的情况下更新页码?我试图将pdf转换为文本,XML和JSON,然后修复页码。但是,如果我将其转换回pdf,它看起来很凌乱(无法保留原始pdf的样式)。我遇到的问题是:

  1. 删除旧页码。
  2. 添加新的页码。

我在Ubuntu上使用python。我曾尝试 ReportLabPyXpyfpdf

Pre*_*eto 3

我也遇到过类似的问题,老实说我无法完全解决它,而是我获取了相应的html并用BeautifulSoup对其进行了处理。然而,我确实得到了比 python 模块更接近的方法,我使用 poppler 中的 pdftotext.exe (底部链接)来读取 pdf 文件,并且它工作得很好,除了它无法区分文本列之外。由于这不是 python 模块,因此我使用 os.system 来调用 .exe 文件上的命令字符串。

def call_poppler(input_pdf, input_path):

    """
    Call poppler to generate a txt file
    """
    command_row = input_path + " " + input_pdf
    os.system(command_row)
    txt_name = input_pdf[0:-4] + ".txt"
    processed_paper = open_txt(txt_name)
    return processed_paper

def open_txt(input_txt_name):

    """
    Open and generate a python object out of the
    txt attained with poppler
    """
    opened_file = open(input_txt_name,"rb").readlines()
    output_file = []
    for row in opened_file:
        row = row.decode("utf-8").strip()
        output_file.append(row)
    return output_file
Run Code Online (Sandbox Code Playgroud)

这会返回一个经过处理的“.txt”文件,然后您可以根据需要进行处理,并使用某些模块(例如 pypdf)将其重写为 pdf,如果这不是您想要的答案,很抱歉,但是 pdf 文件很难在其中处理python,因为它们不是基于文本的文件。不要忘记给出可执行文件的路径。您可以在这里获取 poppler:https://poppler.freedesktop.org/