【Python】使用Python在win10下将PDF转化为图片

需要安装的Python模块

pip install wand
pip install pypdf2

需要额外安装的软件

  • ImageMagick:地址
  • GhostScript:地址
    注意下载与自己的Python解释器和系统对应的版本(32位或者64位)

示例代码

import io
from wand.image import Image
from wand.color import Color
from PyPDF2 import PdfFileReader, PdfFileWriter

filename = './test.pdf'

pdf = PdfFileReader(filename)

if pdf is not None:

    print('convert starting...')

    pages = pdf.getNumPages()

    for index in range(pages):

        page_now = pdf.getPage(index)

        dst = PdfFileWriter()
        dst.addPage(page_now)

        pdf_bytes = io.BytesIO()
        dst.write(pdf_bytes)
        pdf_bytes.seek(0)

        img = Image(file=pdf_bytes, resolution=200)

        img.format = 'jpg'
        img.compression_quality = 100
        # img.background_color = Color('white')
        # img.font_color = Color('red')

        path = ("./output/%s_%d.jpg" % (filename, index))
        img.save(filename=path)
        img.destroy()
        
        print("page %s_%d finished." % (filename, index))

效果图片

【Python】使用Python在win10下将PDF转化为图片_第1张图片

我遇到的问题

转化文件的时候,遇到一下报错信息

Traceback (most recent call last):
  File "c:\Users\xx\.vscode\extensions\ms-python.python-2019.3.6558\pythonFiles\ptvsd_launcher.py", line 45, in <module>
    main(ptvsdArgs)
  File "c:\Users\xx\.vscode\extensions\ms-python.python-2019.3.6558\pythonFiles\lib\python\ptvsd\__main__.py", line 391, in main
    run()
  File "c:\Users\xx\.vscode\extensions\ms-python.python-2019.3.6558\pythonFiles\lib\python\ptvsd\__main__.py", line 272, in run_file
    runpy.run_path(target, run_name='__main__')
  File "C:\Python3\lib\runpy.py", line 263, in run_path
    pkg_name=pkg_name, script_name=fname)
  File "C:\Python3\lib\runpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "C:\Python3\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "c:\Users\xx\Desktop\convert.py", line 24, in <module>
    dst.write(pdf_bytes)
  File "C:\Python3\lib\site-packages\PyPDF2\pdf.py", line 501, in write
    obj.writeToStream(stream, key)
  File "C:\Python3\lib\site-packages\PyPDF2\generic.py", line 549, in writeToStream writeToStream
    value.writeToStream(stream, encryption_key)                       writeToStream
  File "C:\Python3\lib\site-packages\PyPDF2\generic.py", line 472, in writeToStream    stream.write(b_(self))
  File "C:\Python3\lib\site-packages\PyPDF2\utils.py", line 238, in b_
    r = s.encode('latin-1')

解决方法:
将报错信息中提到的文件Python3/lib/site-packages/PyPDF2/utils.py中的r=s.encode('latin-1')改为utf-8,问题解决。

你可能感兴趣的:(一入Python深似海)