MCPcopy
hub / github.com/dilshod/xlsx2csv / convert

Method convert

xlsx2csv.py:244–298  ·  view source on GitHub ↗

outfile - path to file or filehandle

(self, outfile, sheetid=1, sheetname=None)

Source from the content-addressed store, hash-verified

242 return None
243
244 def convert(self, outfile, sheetid=1, sheetname=None):
245 """outfile - path to file or filehandle"""
246 if sheetname:
247 sheetid = self.getSheetIdByName(sheetname)
248 if not sheetid:
249 raise XlsxException("Sheet '%s' not found" % sheetname)
250 if sheetid > 0:
251 self._convert(sheetid, outfile)
252 else:
253 if isinstance(outfile, str):
254 if not os.path.exists(outfile):
255 os.makedirs(outfile)
256 elif os.path.isfile(outfile):
257 raise OutFileAlreadyExistsException("File " + str(outfile) + " already exists!")
258 for s in self.workbook.sheets:
259 sheetname = s['name']
260 sheetstate = s['state']
261
262 # filter hidden sheets
263 if sheetstate in ('hidden', 'veryHidden') and self.options['exclude_hidden_sheets']:
264 continue
265
266 # filter sheets by include pattern
267 include_sheet_pattern = self.options['include_sheet_pattern']
268 if type(include_sheet_pattern) == type(""): # optparser lib fix
269 include_sheet_pattern = [include_sheet_pattern]
270 if len(include_sheet_pattern) > 0:
271 include = False
272 for pattern in include_sheet_pattern:
273 include = pattern and len(pattern) > 0 and re.match(pattern, sheetname)
274 if include:
275 break
276 if not include:
277 continue
278
279 # filter sheets by exclude pattern
280 exclude_sheet_pattern = self.options['exclude_sheet_pattern']
281 if type(exclude_sheet_pattern) == type(""): # optparser lib fix
282 exclude_sheet_pattern = [exclude_sheet_pattern]
283 exclude = False
284 for pattern in exclude_sheet_pattern:
285 exclude = pattern and len(pattern) > 0 and re.match(pattern, sheetname)
286 if exclude:
287 break
288 if exclude:
289 continue
290
291 if not self.py3:
292 sheetname = sheetname.encode('utf-8')
293 of = outfile
294 if isinstance(outfile, str):
295 of = os.path.join(outfile, sheetname + '.csv')
296 elif self.options['sheetdelimiter'] and len(self.options['sheetdelimiter']):
297 of.write(self.options['sheetdelimiter'] + " " + str(s['index']) + " - " + sheetname + self.options['lineterminator'])
298 self._convert(s['index'], of)
299
300 def _convert(self, sheet_index, outfile):
301 closefile = False

Callers 2

mainFunction · 0.95
convert_recursiveFunction · 0.80

Calls 4

getSheetIdByNameMethod · 0.95
_convertMethod · 0.95
XlsxExceptionClass · 0.85

Tested by

no test coverage detected