#!/usr/bin/env python import agate import agatedbf import agateexcel from csvkit import convert from csvkit.convert.fixed import fixed2csv from csvkit.convert.geojs import geojson2csv from csvkit.cli import CSVKitUtility agatedbf.patch() agateexcel.patch() SUPPORTED_FORMATS = ['csv', 'dbf', 'fixed', 'geojson', 'json', 'ndjson', 'xls', 'xlsx'] class In2CSV(CSVKitUtility): description = 'Convert common, but less awesome, tabular data formats to CSV.' epilog = 'Some command-line flags only pertain to specific input formats.' override_flags = ['f'] def add_arguments(self): self.argparser.add_argument(metavar="FILE", nargs='?', dest='input_path', help='The CSV file to operate on. If omitted, will accept input on STDIN.') self.argparser.add_argument('-f', '--format', dest='filetype', help='The format of the input file. If not specified will be inferred from the file type. Supported formats: %s.' % ', '.join(sorted(SUPPORTED_FORMATS))) self.argparser.add_argument('-s', '--schema', dest='schema', help='Specifies a CSV-formatted schema file for converting fixed-width files. See documentation for details.') self.argparser.add_argument('-k', '--key', dest='key', help='Specifies a top-level key to use look within for a list of objects to be converted when processing JSON.')
#!/usr/bin/env python import agate import agateexcel import six agateexcel.patch() def xlsx2csv(f, **kwargs): """ Convert an Excel .xlsx file to csv. """ table = agate.Table.from_xlsx(f, sheet=kwargs.get('sheet', None)) o = six.StringIO() output = table.to_csv(o) output = o.getvalue() o.close() return output