Skip to content

Commit ca24b29

Browse files
dilshodclaude
andcommitted
Add type hints to xlsx2csv API and update changelog
- Add backwards-compatible type hints to main Xlsx2csv class methods - Include type hints for __init__, convert, getSheetIdByName, and context manager methods - Add type hints to convert_recursive function - Add typing imports with fallback for Python 2.4+ compatibility - Update changelog with PR #298 fix 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 234f313 commit ca24b29

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
version 0.8.4 (2025-Oct-1):
22
* Add --continue-on-error option for batch processing to continue when individual files fail
33
* Fix ResourceWarning by adding context manager support and proper file handle cleanup
4+
* Fix invalid name value - Excel uses #NAME? not #NAME! (#298)
45

56
version 0.8.3 (2024-Jul-29):
67
* Fix bug when missing workbook relationship

xlsx2csv.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,21 @@
4242
# python2.4
4343
from optparse import OptionParser
4444

45+
try:
46+
from typing import Union, Optional, Dict, Any, IO, List, TextIO, BinaryIO
47+
from types import TracebackType
48+
except ImportError:
49+
# python2.4 or older versions without typing
50+
Union = None
51+
Optional = None
52+
Dict = None
53+
Any = None
54+
IO = None
55+
List = None
56+
TextIO = None
57+
BinaryIO = None
58+
TracebackType = None
59+
4560
# see also ruby-roo lib at: http://github.com/hmcgowan/roo
4661
FORMATS = {
4762
'general': 'float',
@@ -183,6 +198,7 @@ class Xlsx2csv:
183198
"""
184199

185200
def __init__(self, xlsxfile, **options):
201+
# type: (Union[str, IO[bytes]], **Any) -> None
186202
options.setdefault("delimiter", ",")
187203
options.setdefault("quoting", csv.QUOTE_MINIMAL)
188204
options.setdefault("sheetdelimiter", "--------")
@@ -239,28 +255,33 @@ def __init__(self, xlsxfile, **options):
239255
self.shared_strings.escape_strings()
240256

241257
def __enter__(self):
258+
# type: () -> Xlsx2csv
242259
return self
243260

244261
def __exit__(self, exc_type, exc_val, exc_tb):
262+
# type: (Optional[type], Optional[Exception], Optional[TracebackType]) -> None
245263
self.close()
246264

247265
def __del__(self):
248266
# Fallback cleanup, but prefer explicit close() or using as context manager
249267
self.close()
250268

251269
def close(self):
270+
# type: () -> None
252271
"""Explicitly close the underlying zip file handle."""
253272
if self.ziphandle:
254273
self.ziphandle.close()
255274
self.ziphandle = None
256275

257276
def getSheetIdByName(self, name):
277+
# type: (str) -> Optional[int]
258278
for s in self.workbook.sheets:
259279
if s['name'] == name:
260280
return s['index']
261281
return None
262282

263283
def convert(self, outfile, sheetid=1, sheetname=None):
284+
# type: (Union[str, TextIO], int, Optional[str]) -> None
264285
"""outfile - path to file or filehandle"""
265286
if sheetname:
266287
sheetid = self.getSheetIdByName(sheetname)
@@ -1091,6 +1112,7 @@ def _range(self, rangeStr):
10911112

10921113

10931114
def convert_recursive(path, sheetid, outfile, kwargs, continue_on_error=False):
1115+
# type: (str, int, Union[str, TextIO], Dict[str, Any], bool) -> None
10941116
for name in os.listdir(path):
10951117
fullpath = os.path.join(path, name)
10961118
if os.path.isdir(fullpath):

0 commit comments

Comments
 (0)