示例#1
0
 def test_fails_on_ods_from_excel(self):
     excel_path = dev_test.path_to_test_data('valid_customers.xls')
     try:
         list(rowio.ods_rows(excel_path))
         self.fail()
     except errors.DataFormatError as anticipated_error:
         dev_test.assert_fnmatches(self, str(anticipated_error), '*: cannot uncompress ODS spreadsheet: *')
示例#2
0
 def test_fails_on_ods_without_content_xml(self):
     broken_ods_path = dev_test.path_to_test_data('broken_without_content_xml.ods')
     try:
         list(rowio.ods_rows(broken_ods_path))
         self.fail('expected DataFormatError')
     except errors.DataFormatError as error:
         error_message = '%s' % error
         self.assertTrue(
             'cannot extract content.xml' in error_message, 'error_message=%r' % error_message)
示例#3
0
 def test_fails_on_ods_from_csv(self):
     broken_ods_path = dev_test.path_to_test_data('customers.csv')
     try:
         list(rowio.ods_rows(broken_ods_path))
         self.fail('expected DataFormatError')
     except errors.DataFormatError as error:
         error_message = '%s' % error
         self.assertTrue(
             'cannot uncompress ODS spreadsheet:' in error_message, 'error_message=%r' % error_message)
示例#4
0
 def test_fails_on_non_existent_ods_sheet(self):
     ods_path = dev_test.path_to_test_data('valid_customers.ods')
     try:
         list(rowio.ods_rows(ods_path, 123))
         self.fail('expected DataFormatError')
     except errors.DataFormatError as error:
         error_message = '%s' % error
         self.assertTrue(
             'ODS must contain at least' in error_message, 'error_message=%r' % error_message)
示例#5
0
def to_csv(ods_source_path, csv_target_path, dialect='excel', sheet=1):
    """
    Convert ODS file in `odsFilePath` to CSV using `dialect` and store the result in `csvTargetPath`.
    """
    assert ods_source_path is not None
    assert csv_target_path is not None
    assert dialect is not None
    assert sheet is not None
    assert sheet >= 1

    with io.open(csv_target_path, 'w', newline='', encoding='utf-8') as csv_target_file:
        csv_writer = _compat.csv_writer(csv_target_file, dialect)
        csv_writer.writerows(rowio.ods_rows(ods_source_path, sheet))
示例#6
0
def toCsv(odsFilePath, csvTargetPath, dialect="excel", sheet=1):
    """
    Convert ODS file in `odsFilePath` to CSV using `dialect` and store the result in `csvTargetPath`.
    """
    assert odsFilePath is not None
    assert csvTargetPath is not None
    assert dialect is not None
    assert sheet is not None
    assert sheet >= 1

    with io.open(csvTargetPath, 'w', newline='',
                 encoding='utf-8') as csvTargetFile:
        csv_writer = _compat.csv_writer(csvTargetFile, dialect)
        csv_writer.writerows(rowio.ods_rows(odsFilePath, sheet))
示例#7
0
 def _raw_rows(self):
     data_format = self.cid.data_format
     format = data_format.format
     if format == data.FORMAT_EXCEL:
         return rowio.excel_rows(self._source_data_stream_or_path, data_format.sheet)
     elif format == data.FORMAT_DELIMITED:
         return rowio.delimited_rows(self._source_data_stream_or_path, data_format)
     elif format == data.FORMAT_FIXED:
         return rowio.fixed_rows(
             self._source_data_stream_or_path, data_format.encoding, interface.field_names_and_lengths(self.cid),
             data_format.line_delimiter)
     elif format == data.FORMAT_ODS:
         return rowio.ods_rows(self._source_data_stream_or_path, data_format.sheet)
     else:
         assert False, 'format=%r' % format
示例#8
0
 def _raw_rows(self):
     data_format = self.cid.data_format
     format = data_format.format
     if format == data.FORMAT_EXCEL:
         return rowio.excel_rows(self._source_data_stream_or_path, data_format.sheet)
     elif format == data.FORMAT_DELIMITED:
         return rowio.delimited_rows(self._source_data_stream_or_path, data_format)
     elif format == data.FORMAT_FIXED:
         return rowio.fixed_rows(
             self._source_data_stream_or_path, data_format.encoding, interface.field_names_and_lengths(self.cid),
             data_format.line_delimiter)
     elif format == data.FORMAT_ODS:
         return rowio.ods_rows(self._source_data_stream_or_path, data_format.sheet)
     else:
         assert False, 'format=%r' % format
示例#9
0
def to_rst(ods_source_path,
           rst_target_path,
           first_row_is_heading=True,
           sheet=1):
    """
    Convert ODS file in `odsFilePath` to reStructuredText and store the result in `rstTargetPath`.
    """
    assert ods_source_path is not None
    assert rst_target_path is not None
    assert sheet >= 1

    rows = list(rowio.ods_rows(ods_source_path, sheet))

    # Find out the length of each column.
    lengths = []
    is_first_row = True
    for row in rows:
        for column_index in range(len(row)):
            item = row[column_index]
            item_length = len(item)
            if is_first_row or (column_index == len(lengths)):
                lengths.append(item_length)
                is_first_row = False
            elif lengths[column_index] < item_length:
                lengths[column_index] = item_length

    if not lengths:
        raise ValueError("file must contain columns: \"%s\"" % ods_source_path)
    for column_index in range(len(lengths)):
        if lengths[column_index] == 0:
            raise ValueError("column %d in file %r must not always be empty" %
                             (column_index + 1, ods_source_path))

    with io.open(rst_target_path, "w", encoding='utf-8') as rst_target_file:
        is_first_row = first_row_is_heading
        _write_rst_separator_line(rst_target_file, lengths, "-")
        for row in rows:
            _write_rst_row(rst_target_file, lengths, row)
            if is_first_row:
                line_separator = "="
                is_first_row = False
            else:
                line_separator = "-"
            _write_rst_separator_line(rst_target_file, lengths, line_separator)
示例#10
0
def to_rst(ods_source_path, rst_target_path, first_row_is_heading=True, sheet=1):
    """
    Convert ODS file in `odsFilePath` to reStructuredText and store the result in `rstTargetPath`.
    """
    assert ods_source_path is not None
    assert rst_target_path is not None
    assert sheet >= 1

    rows = list(rowio.ods_rows(ods_source_path, sheet))

    # Find out the length of each column.
    lengths = []
    is_first_row = True
    for row in rows:
        for column_index in range(len(row)):
            item = row[column_index]
            item_length = len(item)
            if is_first_row or (column_index == len(lengths)):
                lengths.append(item_length)
                is_first_row = False
            elif lengths[column_index] < item_length:
                lengths[column_index] = item_length

    if not lengths:
        raise ValueError("file must contain columns: \"%s\"" % ods_source_path)
    for column_index in range(len(lengths)):
        if lengths[column_index] == 0:
            raise ValueError("column %d in file %r must not always be empty" % (column_index + 1, ods_source_path))

    with io.open(rst_target_path, "w", encoding='utf-8') as rst_target_file:
        is_first_row = first_row_is_heading
        _write_rst_separator_line(rst_target_file, lengths, "-")
        for row in rows:
            _write_rst_row(rst_target_file, lengths, row)
            if is_first_row:
                line_separator = "="
                is_first_row = False
            else:
                line_separator = "-"
            _write_rst_separator_line(rst_target_file, lengths, line_separator)
示例#11
0
 def test_can_read_ods_rows(self):
     ods_path = dev_test.path_to_test_data('valid_customers.ods')
     self._assert_rows_contain_data(rowio.ods_rows(ods_path))