Skip to content

Commit

Permalink
Make reading Excel sheets in DataTable more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
Cito committed Mar 5, 2023
1 parent 25179e5 commit 91b3898
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions webware/MiscUtils/DataTable.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
John 893-5901
This data often comes from delimited text files which typically
have well defined columns or fields with several rows each of which can
have well-defined columns or fields with several rows each of which can
be thought of as a record.
Using a DataTable can be as easy as using lists and dictionaries::
Expand Down Expand Up @@ -202,6 +202,7 @@

from datetime import date, datetime, time, timedelta, tzinfo
from decimal import Decimal
from time import sleep
from warnings import warn

from MiscUtils import NoDefault
Expand Down Expand Up @@ -249,8 +250,8 @@

def canReadExcel():
try:
from win32com.client import Dispatch # pylint: disable=import-error
Dispatch("Excel.Application")
from win32com import client # pylint: disable=import-error
client.gencache.EnsureDispatch("Excel.Application")
except Exception:
return False
return True
Expand Down Expand Up @@ -440,9 +441,19 @@ def canReadExcel():
def readExcel(self, worksheet=1, row=1, column=1):
maxBlankRows = 10
numRowsToReadPerCall = 20
from win32com.client import Dispatch # pylint: disable=import-error
excel = Dispatch("Excel.Application")
workbook = excel.Workbooks.Open(os.path.abspath(self._filename))
from win32com import client # pylint: disable=import-error
for _retry in range(40):
try:
excel = client.gencache.EnsureDispatch("Excel.Application")
workbooks = excel.Workbooks
except Exception:
# Excel may be busy, wait a bit and try again
sleep(0.125)
else:
break
else:
raise DataTableError('Cannot read Excel sheet')
workbook = workbooks.Open(os.path.abspath(self._filename))
try:
worksheet = workbook.Worksheets(worksheet)
worksheet.Cells(row, column)
Expand Down

0 comments on commit 91b3898

Please sign in to comment.