Insert Image using Openpyxl [Errno 2] No such file or directory

Hi All,

I need to insert an image in an excel workbook using Openpyxl. Image file is available in File Manager but not working. Getting this below error.

Code :
import openpyxl
wb = openpyxl.Workbook()
ws = wb.worksheets[0]
img = openpyxl.drawing.image.Image(‘test_image.png’)
img.anchor = ‘A1’
ws.add_image(img)
wb.save(‘test.xlsx’)

Error:
Traceback (most recent call last):
File “/home/dhasa/uat-site/frappe-bench/apps/frappe/frappe/app.py”, line 64, in application
response = frappe.handler.handle()
File “/home/dhasa/uat-site/frappe-bench/apps/frappe/frappe/handler.py”, line 24, in handle
data = execute_cmd(cmd)
File “/home/dhasa/uat-site/frappe-bench/apps/frappe/frappe/handler.py”, line 64, in execute_cmd
return frappe.call(method, **frappe.form_dict)
File “/home/dhasa/uat-site/frappe-bench/apps/frappe/frappe/init.py”, line 1105, in call
return fn(*args, **newargs)
File “/home/dhasa/uat-site/frappe-bench/apps/upro_erp/upro_erp/custom.py”, line 119, in download_sales_invoice_xls
xlsx_file = make_xlsx(filename)
File “/home/dhasa/uat-site/frappe-bench/apps/upro_erp/upro_erp/custom.py”, line 129, in make_xlsx
img = Image(“test_image.png”)
File “/home/dhasa/uat-site/frappe-bench/env/lib/python3.8/site-packages/openpyxl/drawing/image.py”, line 34, in init
image = _import_image(img)
File “/home/dhasa/uat-site/frappe-bench/env/lib/python3.8/site-packages/openpyxl/drawing/image.py”, line 18, in _import_image
img = PILImage.open(img)
File “/home/dhasa/uat-site/frappe-bench/env/lib/python3.8/site-packages/PIL/Image.py”, line 2766, in open
fp = builtins.open(filename, “rb”)
FileNotFoundError: [Errno 2] No such file or directory: ‘test_image.png’

Found Solution

import openpyxl
wb = openpyxl.Workbook()
ws = wb.worksheets[0]
url = get_url(“/files/test_image.png”)
import io
import urllib3
http = urllib3.PoolManager()
r = http.request(‘GET’, url)
image_file = io.BytesIO(r.data)
img = openpyxl.drawing.image.Image(image_file)
img.anchor = ‘A2’
ws.add_image(img)
wb.save(‘test.xlsx’)