栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

[python]excel中url转图片

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

[python]excel中url转图片

遇到的一个问题
图片url是webp格式,下载图片后,插入excel报错
报错信息

Traceback (most recent call last):
  File "excel_utils.py", line 86, in 
    obj.process_push_res("./uncheck_2021-10-12.xlsx", "./res1.xlsx")
  File "excel_utils.py", line 81, in process_push_res
    self.url2pic(target_file, target_file, sheet_name, col)
  File "excel_utils.py", line 66, in url2pic
    wb.save(target_file)
  File "/home/gzh/.local/lib/python3.7/site-packages/openpyxl/workbook/workbook.py", line 392, in save
    save_workbook(self, filename)
  File "/home/gzh/.local/lib/python3.7/site-packages/openpyxl/writer/excel.py", line 293, in save_workbook
    writer.save()
  File "/home/gzh/.local/lib/python3.7/site-packages/openpyxl/writer/excel.py", line 275, in save
    self.write_data()
  File "/home/gzh/.local/lib/python3.7/site-packages/openpyxl/writer/excel.py", line 94, in write_data
    self.manifest._write(archive, self.workbook)
  File "/home/gzh/.local/lib/python3.7/site-packages/openpyxl/packaging/manifest.py", line 181, in _write
    self._register_mimetypes(filenames=archive.namelist())
  File "/home/gzh/.local/lib/python3.7/site-packages/openpyxl/packaging/manifest.py", line 193, in _register_mimetypes
    mime = mimetypes.types_map[True][ext]
KeyError: '.webp'

原因:图片下载后,保存为后缀为.jpeg格式的文件,实际上该图片的格式还是.webp
解决方案:图片下载完成后,加一步格式转换,强制转换成jpeg

 # download_img
 res = requests.get(url)
 img = open(file_name, 'wb')
 img.write(res.content)
 img.close()
 ## 格式转换
 img = PIL.Image.open(file_name).convert("RGB")
 img.save(file_name, 'jpeg')

Image Conversion (JPG ⇄ PNG/JPG ⇄ WEBP) with Python

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/321833.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号