Python转换PDF

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (260)

我有以下代码从单个多页PDF创建多个jpgs。但是我收到以下错误:wand.exceptions.BlobError: unable to open image '{uuid}.jpg': No such file or directory @ error/blob.c/OpenBlob/2841但图像已创建。我最初认为它可能是一种竞争条件,所以我放入了一个time.sleep()但是也没有用,所以我不相信它就是这样。有没有人见过这个?

def split_pdf(pdf_obj, step_functions_client, task_token):
    print(time.time())

    read_pdf = PyPDF2.PdfFileReader(pdf_obj)
    images = []

    for page_num in range(read_pdf.numPages):
        output = PyPDF2.PdfFileWriter()
        output.addPage(read_pdf.getPage(page_num))

        generateduuid = str(uuid.uuid4())
        filename = generateduuid + ".pdf"
        outputfilename = generateduuid + ".jpg"
        with open(filename, "wb") as out_pdf:
            output.write(out_pdf) # write to local instead

        image = {"page": str(page_num + 1)}  # Start at 1 rather than 0

        create_image_process = subprocess.Popen(["gs", "-o " + outputfilename, "-sDEVICE=jpeg", "-r300", "-dJPEGQ=100", filename], stdout=subprocess.PIPE)
        create_image_process.wait()

        time.sleep(10)
        with(Image(filename=outputfilename)) as img:
            image["image_data"] = img.make_blob('jpeg')
            image["height"] = img.height
            image["width"] = img.width
            images.append(image)

            if hasattr(step_functions_client, 'send_task_heartbeat'):
                step_functions_client.send_task_heartbeat(taskToken=task_token)

    return images
提问于
用户回答回答于

当您尝试首先打开PDF时,看起来您没有传入值 - 因此您收到的错误。

确保使用完整文件路径格式化字符串,例如f'/path/to/file/{uuid}.jpg''/path/to/file/{}.jpg'.format(uuid)

扫码关注云+社区

领取腾讯云代金券

年度创作总结 领取年终奖励