【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))

效果图片

在这里插入图片描述

我遇到的问题

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

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,问题解决。

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值