简体   繁体   English

pyPDF2 PdfFileWriter output 返回损坏的文件

[英]pyPDF2 PdfFileWriter output returns a corrupted file

I am very new to python. I have the following code that takes user input from a GUI for the "x" and "a" variable.我是 python 的新手。我有以下代码,它从 GUI 获取用户输入的“x”和“a”变量。 The goal is to have it open each.pdf in the directory perform the modifications, and save over itself.目标是让它在目录中打开 each.pdf 执行修改,并保存自己。 Each pdf in the directory is a single page pdf. It seems to work however, the newly saved file is corrupted and cannot be opened.目录下的每个pdf都是单页pdf,貌似可以,但是新保存的文件已经损坏,打不开。

Seal_pdf = PdfFileReader(open(state, "rb"), strict=False)
input_pdf = glob.glob(os.path.join(x, '*.pdf'))
output_pdf = PdfFileWriter()
page_count = len(fnmatch.filter(os.listdir(x), '*.pdf'))
i = 0

if a == "11x17":
    for file in input_pdf:
        sg.OneLineProgressMeter('My Meter', i, page_count, 'And now we Wait.....')
        PageObj = PyPDF2.PdfFileReader(open(file, "rb"), strict=False).getPage(0)
        PageObj.scaleTo(11*72, 17*72)
        PageObj.mergePage(Seal_pdf.getPage(0))
        output_filename = f"{file}"
        f = open(output_filename, "wb+")
        output_pdf.write(f)
        i = i + 1

Adding output_pdf.addPage(PageObj) to the loop produces and uncorrupted file however, that causes each successive.pdf to be added to the previous.pdf.然而,将output_pdf.addPage(PageObj)添加到循环中会生成未损坏的文件,这会导致每个连续的 .pdf 添加到前一个 .pdf。 (ex. "pdf 1" is only "pdf 1", "pdf2 is now two pages "pdf1" and "pdf2" merged, etc.). I also attempted to change the next to last two lines to (例如,“pdf 1”只是“pdf 1”,“pdf2 现在是两页”pdf1”和“pdf2”合并等)。我还尝试将倒数第二行更改为

with open(output_filename, "wb+") as f:
    output_pdf.write(f)

with no luck.没有运气。 I can't figure out what I am missing to have the PdfFileWriter return a single page, uncorrupted file for each individual pdf in the directory.我无法弄清楚让 PdfFileWriter 为目录中的每个单独的 pdf 返回一个页面,未损坏的文件我缺少什么。

if a == "11x17":
    for file in input_pdf:
        sg.OneLineProgressMeter('My Meter', i, page_count, 'And now we Wait.....')
        PageObj = PyPDF2.PdfFileReader(open(file, "rb"), strict=False).getPage(0)
        PageObj.scaleTo(11*72, 17*72)
        PageObj.mergePage(Seal_pdf.getPage(0))
        output_pdf.addPage(PageObj)
        output_filename = f"{file}"
        f = open(output_filename, "wb+")
        output_pdf.write(f)
        i = i + 1

I was able to solve this finally by simply putting the output_pdf = PdfFileWriter() inside the loop.我最终能够通过简单地将output_pdf = PdfFileWriter()放入循环中来解决这个问题。 I stumbled across that being the solution for another loop issue and thought I would try it.我偶然发现这是另一个循环问题的解决方案,并认为我会尝试一下。 PdfFileWriter() inside loop PdfFileWriter() 内部循环

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM