需要安装的Python模块
pip install wand
pip install pypdf2
需要额外安装的软件
示例代码
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
,问题解决。