LoginSignup
2
1

More than 5 years have passed since last update.

[Python] OpenPyXLのadd_imageでExcelオープン時にエラーが出る場合の対処法

Posted at

OpenPyXLを使ったExcelへの画像貼り付けでExcelファイルの修復エラーが出た際に難儀したのでその対処方法を残します。

サンプルコード

~~~
img = px.drawing.image.Image("sample.xlsx")
ws.add_image(img,'A1')
wb.save('sample.xlsx')

エラー

'sample.xlsx'の一部の内容に問題が見つかりました。可能な限り回復しますか?ブックの発行元が信頼できる場合は、「はい」をクリックしてください。

対処法

OpenPyXLのバージョンを確認する。

>pip show openpyxl
Name: openpyxl
Version: 2.5.1
Summary: A Python library to read/write Excel 2010 xlsx/xlsm files
Home-page: https://openpyxl.readthedocs.io
Author: See AUTHORS
Author-email: charlie.clark@clark-consulting.eu
License: MIT/Expat
Location:
Requires: jdcal, et-xmlfile
Required-by:

OpenPyXLのバージョンが低いと上記のエラーが出ます。
最新バージョンのOpenPyXLをインストールすることで上記エラーを回避できます。

>pip install openpyxl==2.5.9
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1