一、先上代码
方式一直接读取pdf报告转图片
import pdf2image
from PIL import Image
import numpy
pdf_cont = open('aaa.pdf', 'rb').read()
# pdf转图片 可进入convert_from_bytes查看其他参数进行设置
page_imgs = pdf2image.convert_from_bytes(pdf_cont, fmt='jpg')
print(len(page_imgs)) # pdf页数
combined = mage.fromarray(numpy.vstack(page_imgs))
combined.save('111.jpeg') # 保存为111.jpeg
方式二,生成pdf报告时直接保存成jpeg图片
import io
import numpy
import pdf2image
from PIL import Image
from reportlab.lib.pagesizes import letter
from reportlab.platypus import SimpleDocTemplate, Paragraph
from reportlab.lib.styles import getSampleStyleSheet
# create a stream
stream = io.BytesIO()
# generate PDF
doc = SimpleDocTemplate(stream, pagesize=letter,
rightMargin=72, leftMargin=72,
topMargin=72, bottomMargin=18)
Story = []
styles = getSampleStyleSheet()
Story.append(Paragraph('<font size=11>This is a PDF</font>', styles["Normal"]))
doc.build(Story)
# get buffer
pdf_buffer = stream.getbuffer()
# pdf转图片 可进入convert_from_bytes查看其他参数进行设置
page_imgs = pdf2image.convert_from_bytes(pdf_buffer, fmt='jpeg')
print(len(page_imgs))
combined = Image.fromarray(numpy.vstack(page_imgs))
combined.save('111.jpeg')
二、问题
pdf报告转为图片中,需要poppler模块
网站下载:http://blog.alivate.com.au/poppler-windows/
https://poppler.freedesktop.org/
百度网盘链接:https://pan.baidu.com/s/1KNMntT7_3JYqFqNyGeH6Nw
提取码:i52a
下载后将文件解压到C:\Program Files\目录下
然后将poppler下面的 bin 目录添加到系统环境变量PATH中
然后运行代码,要是还是提示PATH的错误,重启计算机再试
缺点,pdf转为图片格式,所花时间相对较大,以后慢慢解决