Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quotes in formatCode case handled, standard format 22 changed to m/d/yy hh:mm #186

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions xlsx2csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
'h:mm:ss am/pm': 'date',
'h:mm': 'time',
'h:mm:ss': 'time',
'm/d/yy h:mm': 'date',
'm/d/yy hh:mm': 'date',
'#,##0 ;(#,##0)': 'float',
'#,##0 ;[red](#,##0)': 'float',
'#,##0.00;(#,##0.00)': 'float',
Expand Down Expand Up @@ -106,7 +106,7 @@
19: 'h:mm:ss am/pm',
20: 'h:mm',
21: 'h:mm:ss',
22: 'm/d/yy h:mm',
22: 'm/d/yy hh:mm',
37: '#,##0 ;(#,##0)',
38: '#,##0 ;[red](#,##0)',
39: '#,##0.00;(#,##0.00)',
Expand Down Expand Up @@ -513,7 +513,10 @@ def parse(self, filehandle):
for numFmt in numFmtsElement[0].childNodes:
if numFmt.nodeType == minidom.Node.ELEMENT_NODE:
numFmtId = int(numFmt._attrs['numFmtId'].value)
formatCode = numFmt._attrs['formatCode'].value.lower().replace('\\', '')
formatCode = numFmt._attrs['formatCode'].value.lower()
bad_chars = ['\\', '"']
for bad_char in bad_chars:
formatCode = formatCode.replace(bad_char, '')
self.numFmts[numFmtId] = formatCode

if styles.namespaceURI:
Expand Down Expand Up @@ -809,6 +812,7 @@ def handleCharData(self, data):
if format_type and not format_type in self.ignore_formats:
try:
if format_type == 'date': # date/time
format_str = self.update_time(format_str)
if self.workbook.date1904:
date = datetime.datetime(1904, 1, 1) + datetime.timedelta(float(self.data))
else:
Expand Down Expand Up @@ -852,6 +856,16 @@ def handleCharData(self, data):
# invalid date format
pass

def update_time(self, date_format):
"""
This function converts the h to hh if am pm is not there
:param date_format:
:return: updated_date_format
"""
if not any(time in date_format for time in ['am', 'pm']):
date_format = re.sub(r"(?<=\W)h(?=:)", "hh", date_format) # "h -> hh"
return date_format

def handleStartElement(self, name, attrs):
has_namespace = name.find(":") > 0
if self.in_row and (name == 'c' or (has_namespace and name.endswith(':c'))):
Expand Down