首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Python转换PDF

Python转换PDF
EN

Stack Overflow用户
提问于 2018-07-31 08:08:07
回答 1查看 356关注 0票数 0

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

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2018-07-31 08:23:10

当你首先尝试打开PDF时,看起来你并没有传入一个值--所以你收到了这个错误。

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51603799

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档