Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
'exclude_hidden_sheets': options.exclude_hidden_sheets,
'merge_cells': options.merge_cells,
'outputencoding': options.outputencoding,
'lineterminator': options.lineterminator,
'ignore_formats': options.ignore_formats
}
sheetid = options.sheetid
if options.all:
sheetid = 0
outfile = options.outfile or sys.stdout
try:
if os.path.isdir(options.infile):
convert_recursive(options.infile, sheetid, outfile, kwargs)
else:
xlsx2csv = Xlsx2csv(options.infile, **kwargs)
if options.sheetname:
sheetid = xlsx2csv.getSheetIdByName(options.sheetname)
if not sheetid:
raise XlsxException("Sheet '%s' not found" % options.sheetname)
xlsx2csv.convert(outfile, sheetid)
except XlsxException:
_, e, _ = sys.exc_info()
sys.stderr.write(str(e) + "\n")
sys.exit(1)
sheetid = options.sheetid
if options.all:
sheetid = 0
outfile = options.outfile or sys.stdout
try:
if os.path.isdir(options.infile):
convert_recursive(options.infile, sheetid, outfile, kwargs)
else:
xlsx2csv = Xlsx2csv(options.infile, **kwargs)
if options.sheetname:
sheetid = xlsx2csv.getSheetIdByName(options.sheetname)
if not sheetid:
raise XlsxException("Sheet '%s' not found" % options.sheetname)
xlsx2csv.convert(outfile, sheetid)
except XlsxException:
_, e, _ = sys.exc_info()
sys.stderr.write(str(e) + "\n")
sys.exit(1)
if sheet_file is None:
sheet_path = None
if sheet_path is None:
sheet_path = "/xl/worksheets/worksheet%i.xml" % sheet_index
sheet_file = self._filehandle(sheet_path)
if sheet_file is None:
sheet_path = None
if sheet_path is None and sheet_index == 1:
sheet_path = self.content_types.types["worksheet"]
sheet_file = self._filehandle(sheet_path)
if sheet_file is None:
sheet_path = None
if sheet_file is None and sheet_path is not None:
sheet_file = self._filehandle(sheet_path)
if sheet_file is None:
raise SheetNotFoundException("Sheet %i not found" % sheet_index)
sheet = Sheet(self.workbook, self.shared_strings, self.styles, sheet_file)
try:
relationships_path = os.path.join(os.path.dirname(sheet_path),
"_rels",
os.path.basename(sheet_path) + ".rels")
sheet.relationships = self._parse(Relationships, relationships_path)
sheet.set_dateformat(self.options['dateformat'])
sheet.set_timeformat(self.options['timeformat'])
sheet.set_floatformat(self.options['floatformat'])
sheet.set_skip_empty_lines(self.options['skip_empty_lines'])
sheet.set_skip_trailing_columns(self.options['skip_trailing_columns'])
sheet.set_include_hyperlinks(self.options['hyperlinks'])
sheet.set_merge_cells(self.options['merge_cells'])
sheet.set_scifloat(self.options['scifloat'])
sheet.set_ignore_formats(self.options['ignore_formats'])
if self.options['escape_strings'] and sheet.filedata:
def normalize_tabular_format(project_path):
kwargs = {
'delimiter' : '\t',
'skip_empty_lines' : True,
'outputencoding': 'utf-8',
}
sheetid = 0
for xf in project_path.rglob('*.xlsx'):
xlsx2csv = Xlsx2csv(xf, **kwargs)
with open(xf.with_suffix('.tsv'), 'wt') as f:
try:
xlsx2csv.convert(f, sheetid)
except SheetNotFoundException as e:
log.warning(f'Sheet weirdness in {xf}\n{e}')
ns = len(xlsx2csv.workbook.sheets)
if ns > 1:
message = f'too many sheets ({ns}) in {self.path.as_posix()!r}'
if self.addError(exc.EncodingError(message),
blame='submission',
path=self.path):
logd.error(message)
f = io.StringIO()
try:
xlsx2csv.convert(f, sheetid)
f.seek(0)
gen = csv.reader(f, delimiter='\t')
yield from gen
except SheetNotFoundException as e:
log.warning(f'Sheet weirdness in {self.path}')
log.warning(str(e))
except AttributeError as e:
message = ('Major sheet weirdness (maybe try resaving, '
'probably a bug in the xlsx2csv converter)? '
f'in {self.path}')
if self.addError(exc.EncodingError(message),
blame='submission',
path=self.path):
log.exception(e)
logd.critical(message)
def convert_recursive(path, sheetid, outfile, kwargs):
for name in os.listdir(path):
fullpath = os.path.join(path, name)
if os.path.isdir(fullpath):
convert_recursive(fullpath, sheetid, outfile, kwargs)
else:
outfilepath = outfile
if len(outfilepath) == 0 and fullpath.lower().endswith(".xlsx"):
outfilepath = fullpath[:-4] + 'csv'
print("Converting %s to %s" % (fullpath, outfilepath))
try:
Xlsx2csv(fullpath, **kwargs).convert(outfilepath, sheetid)
except zipfile.BadZipfile:
print("File %s is not a zip file" % fullpath)
def normalize_tabular_format(project_path):
kwargs = {
'delimiter' : '\t',
'skip_empty_lines' : True,
'outputencoding': 'utf-8',
}
sheetid = 0
for xf in project_path.rglob('*.xlsx'):
xlsx2csv = Xlsx2csv(xf, **kwargs)
with open(xf.with_suffix('.tsv'), 'wt') as f:
try:
xlsx2csv.convert(f, sheetid)
except SheetNotFoundException as e:
log.warning(f'Sheet weirdness in {xf}\n{e}')
def xlsx(self):
kwargs = {
'delimiter': '\t',
'skip_empty_lines': True,
'outputencoding': 'utf-8',
'hyperlinks': True,
}
sheetid = 1
try:
xlsx2csv = Xlsx2csv(self.path.as_posix(), **kwargs)
except InvalidXlsxFileException as e:
raise exc.NoDataError(f'{self.path}') from e
ns = len(xlsx2csv.workbook.sheets)
if ns > 1:
message = f'too many sheets ({ns}) in {self.path.as_posix()!r}'
if self.addError(exc.EncodingError(message),
blame='submission',
path=self.path):
logd.error(message)
f = io.StringIO()
try:
xlsx2csv.convert(f, sheetid)
f.seek(0)
gen = csv.reader(f, delimiter='\t')
'workbook',
'worksheet',
'relationships',
}
DEFAULT_APP_PATH = "/xl"
DEFAULT_WORKBOOK_PATH = DEFAULT_APP_PATH + "/workbook.xml"
def eprint(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
class XlsxException(Exception):
pass
class InvalidXlsxFileException(XlsxException):
pass
class SheetNotFoundException(XlsxException):
pass
class OutFileAlreadyExistsException(XlsxException):
pass
class Xlsx2csv:
"""
Usage: Xlsx2csv("test.xslx", **params).convert("test.csv", sheetid=1)
Input:
xlsxfile - path to file or filehandle
DEFAULT_APP_PATH = "/xl"
DEFAULT_WORKBOOK_PATH = DEFAULT_APP_PATH + "/workbook.xml"
def eprint(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
class XlsxException(Exception):
pass
class InvalidXlsxFileException(XlsxException):
pass
class SheetNotFoundException(XlsxException):
pass
class OutFileAlreadyExistsException(XlsxException):
pass
class Xlsx2csv:
"""
Usage: Xlsx2csv("test.xslx", **params).convert("test.csv", sheetid=1)
Input:
xlsxfile - path to file or filehandle
options:
sheetid - sheet no to convert (0 for all sheets)
dateformat - override date/time format
timeformat - override time format