【问题标题】:How to convert pdf from url to image using pdf2image in python?如何在python中使用pdf2image将pdf从url转换为图像?
【发布时间】:2019-10-29 08:49:35
【问题描述】:

我可以使用 pdf2image convert_to_path 将驱动器中的 pdf 文件转换为图像,但是当我尝试对 pdf 'https://example.com/abc.pdf' 进行相同操作时,会出现多个错误。

代码

url = 'https://example.com/abc.pdf'
scrape = urlopen(url)  # for external files
pil_images = pdf2image.convert_from_bytes(scrape.read(), dpi=200, 
             output_folder=None, first_page=None, last_page=None,
             thread_count=1, userpw=None,use_cropbox=False, strict=False,
             poppler_path=r"C:\poppler-0.68.0_x86\poppler-0.68.0\bin",)

错误:

   Unable to get page count. Syntax Error: Document stream is empty

也关注了下面的链接,但没有运气

Python3: Download PDF to memory and convert first page to image

身份验证屏幕截图:

【问题讨论】:

    标签: python scrape poppler


    【解决方案1】:

    首先按照此博客中的说明从 URL 下载 pdf。 https://dzone.com/articles/simple-examples-of-downloading-files-using-python

    如果您的 pdf 中有多个页面,请使用此将 pdf 转换为图像或任何其他系列格式。

    import ghostscript
    
    def pdf2jpeg(pdf_input_path, jpeg_output_path):
        args = ["pdf2jpeg", # actual value doesn't matter
                "-dNOPAUSE",
                "-sDEVICE=jpeg",
                "-r144",
                "-sOutputFile=" + jpeg_output_path,
                pdf_input_path]
        ghostscript.Ghostscript(*args)
    

    参考:Converting a PDF to a series of images with Python

    对于身份验证,试试这个。

    import os
    import requests
    
    from urlparse import urlparse
    
    username = 'foo'
    password = 'sekret'
    
    url = 'http://example.com/blueberry/download/somefile.jpg'
    filename = os.path.basename(urlparse(url).path)
    
    r = requests.get(url, auth=(username,password))
    
    if r.status_code == 200:
       with open(filename, 'wb') as out:
          for bits in r.iter_content():
              out.write(bits)
    

    参考:Download a file providing username and password using Python

    【讨论】:

    • 如何验证 wget.download(url, path) 方法的 url?
    • 您的 pdf 下载网址将包含用户名和密码?
    • 是的,目前是从代码返回401,在浏览器中,输入凭据后才能下载
    • 如果可能的话,您使用哪种类型的身份验证可以上传截图?
    • 改用下面的代码。感谢您的建议。 r = requests.get(url, auth=HttpNtlmAuth('domain\\username',password), stream=True)
    猜你喜欢
    • 2021-01-14
    • 1970-01-01
    • 2021-12-07
    • 2021-05-20
    • 2014-09-14
    • 2020-06-27
    • 2022-06-13
    • 1970-01-01
    • 2016-09-14
    相关资源
    最近更新 更多