从pptx下载WMF并解码为JPEG [英] Download WMF from pptx and decode to JPEG
问题描述
您好stackoverflow用户.
Hello stackoverflow users.
我正在尝试从PowerPoint演示文稿中下载图像,然后对其进行处理(以识别特定坐标上的数字).
I am trying to download an image from the powerpoint presentation and then to process it(to recognize numbers on it at certain coordinates).
我的问题是我只能以.wmf格式从pptx数据下载图像,而无法将其转换.我已经尝试了所有可能的解决方案.
My problem is that I can download an image from pptx data only in .wmf format, and I cannot convert it. I have tried all possible solutions already.
from pptx import Presentation
from pptx.enum.shapes import MSO_SHAPE_TYPE
pptx_path = "name_pptx.pptx"
prs = Presentation(pptx_path)
desired_slide = prs.slides[6 - 1]
for shape in desired_slide.shapes:
if shape.shape_type == MSO_SHAPE_TYPE.PICTURE:
image_file_bytes = shape.image.blob
file_extension = shape.image.ext # at this point format is .wfm
有趣的是,在Powerpoint中,我可以在保存文件时选择所需的.jpeg扩展名.
Interesting that in Powerpoint I can select a desired .jpeg extension when saving a file.
推荐答案
花了我几个小时才能解决我的问题,在Windows中将wmf文件转换为jpg有点棘手.我将图像添加到临时Excel文件中,然后从中下载图像.
It took me few hours to solve my problem, convertation of wmf file to jpg is a bit tricky in Windows. I add the image to temporary excel file, and then download an image from it.
class ExcelHelpers():
@staticmethod
def add_img_to_excel(path_to_wmf):
import xlsxwriter
workbook = xlsxwriter.Workbook('test.xlsx')
worksheet = workbook.add_worksheet()
worksheet.insert_image('A1', path_to_wmf)
workbook.close()
@staticmethod
def get_img_from_excel(long_filename):
filename = os.path.basename(long_filename).split('.')[0]
from PIL import ImageGrab
import win32com.client as win32
excel = win32.gencache.EnsureDispatch('Excel.Application')
path_to_excel = os.path.join(os.getcwd(), 'test.xlsx')
workbook = excel.Workbooks.Open(path_to_excel)
for sheet in workbook.Worksheets:
for i, shape in enumerate(sheet.Shapes):
if shape.Name.startswith('Picture'):
shape.Copy()
image = ImageGrab.grabclipboard()
image.save('{}.jpg'.format(filename), 'jpeg')
workbook.Close()
excel.Quit()
del excel
os.remove(long_filename)
os.remove('test.xlsx')
这篇关于从pptx下载WMF并解码为JPEG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!