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

Method _convert

xlsx2csv.py:300–377  ·  view source on GitHub ↗
(self, sheet_index, outfile)

Source from the content-addressed store, hash-verified

298 self._convert(s['index'], of)
299
300 def _convert(self, sheet_index, outfile):
301 closefile = False
302 if isinstance(outfile, str):
303 if sys.version_info[0] == 2:
304 outfile = open(outfile, 'wb+')
305 elif sys.version_info[0] == 3:
306 outfile = open(outfile, 'w+', encoding=self.options['outputencoding'], newline="")
307 else:
308 raise XlsxException("error: version of your python is not supported: " + str(sys.version_info) + "\n")
309 closefile = True
310 try:
311 writer = csv.writer(outfile, quoting=self.options['quoting'], delimiter=self.options['delimiter'],
312 lineterminator=self.options['lineterminator'])
313
314 sheets_filtered = list(filter(lambda s: s['index'] == sheet_index, self.workbook.sheets))
315 if len(sheets_filtered) == 0:
316 raise XlsxValueError("Sheet with index %i not found or can't be handled" % sheet_index)
317
318 sheet_path = None
319 # using sheet relation information
320 if 'relation_id' in sheets_filtered[0] and sheets_filtered[0]['relation_id'] is not None:
321
322 relation_id = sheets_filtered[0]['relation_id']
323 if relation_id in self.workbook.relationships.relationships and \
324 'target' in self.workbook.relationships.relationships[relation_id]:
325 relationship = self.workbook.relationships.relationships[relation_id]
326 sheet_path = relationship['target']
327 if not (sheet_path.startswith("/xl/") or sheet_path.startswith("xl/")):
328 sheet_path = "/xl/" + sheet_path
329
330 sheet_file = None
331 if sheet_path is None:
332 sheet_path = "/xl/worksheets/sheet%i.xml" % sheet_index
333 sheet_file = self._filehandle(sheet_path)
334 if sheet_file is None:
335 sheet_path = None
336 if sheet_path is None:
337 sheet_path = "/xl/worksheets/worksheet%i.xml" % sheet_index
338 sheet_file = self._filehandle(sheet_path)
339 if sheet_file is None:
340 sheet_path = None
341 if sheet_path is None and sheet_index == 1:
342 sheet_path = self.content_types.types["worksheet"]
343 sheet_file = self._filehandle(sheet_path)
344 if sheet_file is None:
345 sheet_path = None
346 if sheet_file is None and sheet_path is not None:
347 sheet_file = self._filehandle(sheet_path)
348 if sheet_file is None:
349 raise SheetNotFoundException("Sheet %i not found" % sheet_index)
350 sheet = Sheet(self.workbook, self.shared_strings, self.styles, sheet_file)
351 try:
352 relationships_path = os.path.join(os.path.dirname(sheet_path),
353 "_rels",
354 os.path.basename(sheet_path) + ".rels")
355 sheet.relationships = self._parse(Relationships, relationships_path)
356 sheet.set_dateformat(self.options['dateformat'])
357 sheet.set_timeformat(self.options['timeformat'])

Callers 1

convertMethod · 0.95

Calls 15

_filehandleMethod · 0.95
_parseMethod · 0.95
set_dateformatMethod · 0.95
set_timeformatMethod · 0.95
set_floatformatMethod · 0.95
set_skip_empty_linesMethod · 0.95
set_merge_cellsMethod · 0.95
set_scifloatMethod · 0.95
set_ignore_formatsMethod · 0.95
set_skip_hidden_rowsMethod · 0.95

Tested by

no test coverage detected