From 67a67f77b1cf158bdae687c4ae8cc4c0fd226dad Mon Sep 17 00:00:00 2001 From: zhuyongchun Date: Fri, 1 Nov 2024 15:04:56 +0800 Subject: [PATCH 1/2] feat: add support to WPS office --- excel2img/excel2img.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/excel2img/excel2img.py b/excel2img/excel2img.py index 7e2fba8..6dc4312 100644 --- a/excel2img/excel2img.py +++ b/excel2img/excel2img.py @@ -20,12 +20,17 @@ from optparse import OptionParser from pywintypes import com_error from PIL import ImageGrab # Note: PIL >= 3.3.1 required to work well with Excel screenshots +from enum import Enum + +class Application(Enum): + Excel = 'Excel.Application' # Microsoft Office + KET = 'ket.Application' # WPS Office class ExcelFile(object): @classmethod - def open(cls, filename): + def open(cls, filename, application): obj = cls() - obj._open(filename) + obj._open(filename, application) return obj def __init__(self): @@ -39,7 +44,7 @@ def __exit__(self, *args): self.close() return False - def _open(self, filename): + def _open(self, filename, application): excel_pathname = os.path.abspath(filename) # excel requires abspath if not os.path.exists(excel_pathname): raise IOError('No such excel file: %s', filename) @@ -48,10 +53,10 @@ def _open(self, filename): try: # Using DispatchEx to start new Excel instance, to not interfere with # one already possibly running on the desktop - self.app = win32com.client.DispatchEx('Excel.Application') + self.app = win32com.client.DispatchEx(application.value) self.app.Visible = 0 except: - raise OSError('Failed to start Excel') + raise OSError(f'Failed to start {application.name}') try: self.workbook = self.app.Workbooks.Open(excel_pathname, ReadOnly=True) @@ -70,7 +75,7 @@ def close(self): CoUninitialize() -def export_img(fn_excel, fn_image, page=None, _range=None): +def export_img(fn_excel, fn_image, page=None, _range=None, application=Application.Excel): """ Exports images from excel file """ output_ext = os.path.splitext(fn_image)[1].upper() @@ -81,7 +86,7 @@ def export_img(fn_excel, fn_image, page=None, _range=None): if _range is not None and page is not None and '!' not in _range: _range = "'%s'!%s"%(page, _range) - with ExcelFile.open(fn_excel) as excel: + with ExcelFile.open(fn_excel, application) as excel: if _range is None: if page is None: page = 1 try: From b15cf147dabb5e4e088106f111bb0adbf2ab273f Mon Sep 17 00:00:00 2001 From: new Date: Mon, 19 May 2025 20:27:24 +0800 Subject: [PATCH 2/2] =?UTF-8?q?refactor(excel2img):=E4=B8=BA=E5=87=BD?= =?UTF-8?q?=E6=95=B0=E5=8F=82=E6=95=B0=E6=B7=BB=E5=8A=A0=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E6=B3=A8=E8=A7=A3-=20=E5=9C=A8=20=5Fopen=20=E5=92=8C=20export?= =?UTF-8?q?=5Fimg=E5=87=BD=E6=95=B0=E4=B8=AD=EF=BC=8C=E4=B8=BA=20applicati?= =?UTF-8?q?on=20=E5=8F=82=E6=95=B0=E6=B7=BB=E5=8A=A0=20Application=20?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E6=B3=A8=E8=A7=A3=20-=20=E8=BF=99=E4=B8=AA?= =?UTF-8?q?=E6=94=B9=E5=8A=A8=E6=8F=90=E9=AB=98=E4=BA=86=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E7=9A=84=E5=8F=AF=E8=AF=BB=E6=80=A7=E5=92=8C=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E5=AE=89=E5=85=A8=E6=80=A7=EF=BC=8C=E4=BE=BF=E4=BA=8E=E7=90=86?= =?UTF-8?q?=E8=A7=A3=20application=20=E5=8F=82=E6=95=B0=E7=9A=84=E9=A2=84?= =?UTF-8?q?=E6=9C=9F=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- excel2img/excel2img.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/excel2img/excel2img.py b/excel2img/excel2img.py index 6dc4312..900221a 100644 --- a/excel2img/excel2img.py +++ b/excel2img/excel2img.py @@ -44,7 +44,7 @@ def __exit__(self, *args): self.close() return False - def _open(self, filename, application): + def _open(self, filename, application: Application = Application.Excel): excel_pathname = os.path.abspath(filename) # excel requires abspath if not os.path.exists(excel_pathname): raise IOError('No such excel file: %s', filename) @@ -75,7 +75,7 @@ def close(self): CoUninitialize() -def export_img(fn_excel, fn_image, page=None, _range=None, application=Application.Excel): +def export_img(fn_excel, fn_image, page=None, _range=None, application: Application = Application.Excel): """ Exports images from excel file """ output_ext = os.path.splitext(fn_image)[1].upper()