django 调用函数如下,报错位置如下,报错代码如下。
有知道这个错误的小伙伴帮助出个主意。
电脑已经安装wps,系统是Windows server2019
# 截图保存
def create_save_img(file_path, img_name):
# 读取excel内容转换为图片
import pythoncom
from win32com.client import DispatchEx
from PIL import ImageGrab
import os
file_path = os.path.abspath(file_path)
pythoncom.CoInitialize() # excel多线程相关
excel = DispatchEx("Excel.Application") # 启动excel
# del excel #这个地方很重要,杀死excel进程是这个地方起作用
# excel.Visible = False # 不显示Excel,而True可视化
excel.DisplayAlerts = True # 是否显示警告,关闭系统警告(保存时不会弹出窗口)
workbook = excel.workbooks.Open(file_path) # 打开excel
sheet = workbook.worksheets["Sheet1"] # 选择sheet
screen_area = sheet.UsedRange # 有内容的区域
screen_area.CopyPicture() # 复制图片区域
sheet.Paste() # 粘贴
'''
name = str(uuid.uuid4()) # 重命名唯一值
image_name = name[:6]
excel.Selection.ShapeRange.Name = image_name # 将刚刚选择的Shape重命名,避免与已有图片混淆
'''
excel.Selection.ShapeRange.Name = img_name # 将刚刚选择的Shape重命名,避免与已有图片混淆
sheet.Shapes(img_name).Copy() # 选择图片
img = ImageGrab.grabclipboard() # 获取剪贴板的图片数据
img.save(r"D:/static/media/img/" + img_name)
workbook.Close(False) # 关闭Excel文件,不保存
excel.Quit() # 退出excel
del excel # 这个地方很重要,杀死excel进程是这个地方起作用
pythoncom.CoUninitialize() # 关闭多线程
os.remove(file_path)
程序写了备注,报错在函数的
excel = DispatchEx("Excel.Application") # 启动excel
报错内容如下
Exception in thread Thread-11:
Traceback (most recent call last):
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python39\lib\threading.py", line 973, in _bootstrap_inner
self.run()
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python39\lib\threading.py", line 910, in run
self._target(*self._args, **self.kwargs)
File "C:\inetpub\wwwroot\BangzhuoProject\apps\tools\pluginunit.py", line 234, in create_save_img
excel = DispatchEx("Excel.Application") # 启动excel
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python39\lib\site-packages\win32com\client_init.py", line 145, in DispatchEx
dispatch = pythoncom.CoCreateInstanceEx(
pywintypes.com_error: (-2147221005, '无效的类字符串', None, None)
·