|
13 | 13 | John 893-5901 |
14 | 14 |
|
15 | 15 | This data often comes from delimited text files which typically |
16 | | -have well defined columns or fields with several rows each of which can |
| 16 | +have well-defined columns or fields with several rows each of which can |
17 | 17 | be thought of as a record. |
18 | 18 |
|
19 | 19 | Using a DataTable can be as easy as using lists and dictionaries:: |
|
202 | 202 |
|
203 | 203 | from datetime import date, datetime, time, timedelta, tzinfo |
204 | 204 | from decimal import Decimal |
| 205 | +from time import sleep |
205 | 206 | from warnings import warn |
206 | 207 |
|
207 | 208 | from MiscUtils import NoDefault |
|
249 | 250 |
|
250 | 251 | def canReadExcel(): |
251 | 252 | try: |
252 | | - from win32com.client import Dispatch # pylint: disable=import-error |
253 | | - Dispatch("Excel.Application") |
| 253 | + from win32com import client # pylint: disable=import-error |
| 254 | + client.gencache.EnsureDispatch("Excel.Application") |
254 | 255 | except Exception: |
255 | 256 | return False |
256 | 257 | return True |
@@ -440,9 +441,19 @@ def canReadExcel(): |
440 | 441 | def readExcel(self, worksheet=1, row=1, column=1): |
441 | 442 | maxBlankRows = 10 |
442 | 443 | numRowsToReadPerCall = 20 |
443 | | - from win32com.client import Dispatch # pylint: disable=import-error |
444 | | - excel = Dispatch("Excel.Application") |
445 | | - workbook = excel.Workbooks.Open(os.path.abspath(self._filename)) |
| 444 | + from win32com import client # pylint: disable=import-error |
| 445 | + for _retry in range(40): |
| 446 | + try: |
| 447 | + excel = client.gencache.EnsureDispatch("Excel.Application") |
| 448 | + workbooks = excel.Workbooks |
| 449 | + except Exception: |
| 450 | + # Excel may be busy, wait a bit and try again |
| 451 | + sleep(0.125) |
| 452 | + else: |
| 453 | + break |
| 454 | + else: |
| 455 | + raise DataTableError('Cannot read Excel sheet') |
| 456 | + workbook = workbooks.Open(os.path.abspath(self._filename)) |
446 | 457 | try: |
447 | 458 | worksheet = workbook.Worksheets(worksheet) |
448 | 459 | worksheet.Cells(row, column) |
|
0 commit comments