excel2img 模块无法启动 Excel

在使用excel2img模块将excel表格内容转为图片的时候,

'''
将Excel需要发送的内容转为图片
'''
#读取文件
excel_file='./data/{}.xlsx'.format(dt.strftime ("%Y-%m-%d"))
sheet_list=["实时小时数据","历史小时数据趋势图"]
#要保存图片的位置(保存为png格式)
if os.path.dirname(excel_file)=="":
    excel_file=os.getcwd()+"//"+excel_file
img_save=os.path.dirname(excel_file)+"//截图"+"//"
if not os.path.exists(img_save):
    os.makedirs(img_save)
#保存图片
#try:
print("开始截图...")
for i in range(len(sheet_list)):
        excel2img.export_img(excel_file, img_save + sheet_list[i] + ".png", sheet_list[i], None)
'''except:
    print("截图失败")
else:
    print("截图完成,截图保存在截图文件夹中")'''


发生下面的报错信息

Traceback (most recent call last):
  File "D:\Python\Project\Air\venv\Lib\site-packages\excel2img\excel2img.py", line 51, in _open
    self.app = win32com.client.DispatchEx('Excel.Application')
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Python\Project\Air\venv\Lib\site-packages\win32com\client\__init__.py", line 146, in DispatchEx
    dispatch = pythoncom.CoCreateInstanceEx(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
pywintypes.com_error: (-2147221164, '没有注册类', None, None)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\Python\Project\Air\Air.py", line 484, in <module>
    excel2img.export_img(excel_file, img_save + sheet_list[i] + ".png", sheet_list[i], None)
  File "D:\Python\Project\Air\venv\Lib\site-packages\excel2img\excel2img.py", line 84, in export_img
    with ExcelFile.open(fn_excel) as excel:
         ^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Python\Project\Air\venv\Lib\site-packages\excel2img\excel2img.py", line 28, in open
    obj._open(filename)
  File "D:\Python\Project\Air\venv\Lib\site-packages\excel2img\excel2img.py", line 54, in _open
    raise OSError('Failed to start Excel')
OSError: Failed to start Excel

Process finished with exit code 1

求解答

  • 以下回答由chatgpt基于相关博客总结生成:

    很抱歉,参考资料中没有关于excel2img模块的内容,无法提供针对该模块的解决方案。请提供更详细的信息或描述具体问题,以便能够提供更准确的帮助。