Permalink
Browse files

Merge branch 'master' into contenttypes

  • Loading branch information...
dilshod committed Sep 13, 2018
2 parents 65b457b + e2b8d61 commit 674d08dd8eb5301a223e8d1ef5c5fb7e595ed46d
Showing with 24 additions and 5 deletions.
  1. +24 −5 xlsx2csv.py
View
@@ -426,8 +426,8 @@ def parse(self, filehandle):
if numFmt.nodeType == minidom.Node.ELEMENT_NODE:
numFmtId = int(numFmt._attrs['numFmtId'].value)
formatCode = numFmt._attrs['formatCode'].value.lower().replace('\\', '')
self.numFmts[numFmtId] = formatCode
# cellXfs
self.numFmts[numFmtId] = formatCode
if styles.namespaceURI:
cellXfsElement = styles.getElementsByTagNameNS(styles.namespaceURI, "cellXfs")
else:
@@ -438,10 +438,22 @@ def parse(self, filehandle):
continue
if cellXfs._attrs and 'numFmtId' in cellXfs._attrs:
numFmtId = int(cellXfs._attrs['numFmtId'].value)
if self.chk_exists(numFmtId)==None:
numFmtId = int(cellXfs._attrs['applyNumberFormat'].value)
self.cellXfs.append(numFmtId)
else:
self.cellXfs.append(None)
# When Unknown Numformat ID assign applyNumberFormat
def chk_exists(self, numFmtId):
xfs_numfmt = numFmtId
format_str = None
if xfs_numfmt in self.numFmts:
format_str = self.numFmts[xfs_numfmt]
elif xfs_numfmt in STANDARD_FORMATS:
format_str = STANDARD_FORMATS[xfs_numfmt]
return format_str
class SharedStrings:
def __init__(self):
self.parser = None
@@ -682,6 +694,12 @@ def handleCharData(self, data):
format_str = self.styles.numFmts[xfs_numfmt]
elif xfs_numfmt in STANDARD_FORMATS:
format_str = STANDARD_FORMATS[xfs_numfmt]
# get format type
if not format_str:
print("unknown format %s at %d" %(format_str,xfs_numfmt))
return
format_type = None
if format_str in FORMATS:
format_type = FORMATS[format_str]
@@ -707,18 +725,19 @@ def handleCharData(self, data):
self.data = date.strftime(str(self.dateformat))
else:
# ignore ";@", don't know what does it mean right now
# ignore "[$-409]" and similar format codes
dateformat = re.sub(r"\[\$\-\d{1,3}\]", "", format_str, 1). \
# ignore "[$-409], [$-f409], [$-16001]" and similar format codes
dateformat = re.sub(r"\[\$\-[A-z0-9]*\]", "", format_str, 1). \
replace(";@", ""). \
replace("yyyy", "%Y").replace("yy", "%y"). \
replace("hh:mm", "%H:%M").replace("h", "%I").replace("%H%H", "%H").replace("ss", "%S"). \
replace("dddd", "d"). \
replace("dd", "d").replace("d", "%d"). \
replace("am/pm", "%p"). \
replace("mmmm", "%B").replace("mmm", "%b").replace(":mm", ":%M").replace("m", "%m").replace("%m%m", "%m")
self.data = date.strftime(str(dateformat)).strip()
elif format_type == 'time': # time
t = int(round((float(self.data) % 1) * 24*60*60, 6)) # it should be in seconds
d = datetime.time((t / 3600) % 24, (t / 60) % 60, t % 60)
d = datetime.time((t // 3600) % 24, (t // 60) % 60, t % 60)
self.data = d.strftime(self.timeformat)
elif format_type == 'float' and ('E' in self.data or 'e' in self.data):
self.data = str(self.floatformat or '%f') % float(self.data)

0 comments on commit 674d08d

Please sign in to comment.