Ejemplo n.º 1
0
def excel(load=None, save=None, logfile=None):
    """Interact with the excel module to utilize spreadsheets."""
    if logfile:
        if os.path.exists(logfile):
            print('WARNING: The file specified by logfile \'{0}\' already '
                  'exists. Would you like to overwrite it?'
                  .format(logfile))
            while True:
                choice = input('Y/n: ').upper()
                if choice == 'Y' or choice == 'YES' or choice == '':
                    break
                elif choice == 'N' or choice == 'NO':
                    print('Action cancelled due to invalid logfile.')
                    sys.exit(0)
                else:
                    print('Invalid input, please answer \'Y\' if you want to '
                          'overwrite the file, or \'N\' if you do not. Would'
                          'you like to overwite the file \'{0}\'?'
                          .format(logfile))
        stream = open(logfile, 'w', encoding='utf-8')
    else:
        stream = sys.stdout
    if load and save:
        raise ValueError('Cannot load and save at the same time!')
    if load:
        if os.path.exists(load):
            swb = SeedsWorkbook()
            swb.load(load)
        else:
            raise FileNotFoundError('The file \'{0}\' does not exist!'
                                    .format(load))
        swb.save_all_sheets_to_db(stream=stream)
    if save:
        if os.path.exists(save):
            print('WARNING: The file {0} exists. Would you like to overwrite '
                  'it?'.format(save))
            while True:
                choice = input('Y/n:  ').upper()
                if choice == 'Y' or choice == 'YES' or choice == '':
                    break
                elif choice == 'N' or choice == 'NO':
                    print('Save cancelled. Exiting.')
                    sys.exit(0)
                else:
                    print('Invalid input, please answer \'Y\' if you want to '
                          'overwrite the file, or \'N\' if you do not. Would'
                          'you like to overwite the file \'{0}\'?'
                          .format(save))
        swb = SeedsWorkbook()
        print('*** BEGIN saving all data to worksheet \'{0}\'. ***'
              .format(save), file=stream)
        swb.add_all_data_to_sheets(stream=stream)
        swb.beautify_all_sheets()
        swb.save(save)
        print('*** END saving all data to worksheet \'{0}\'. ***'
              .format(save), file=stream)
    if stream is not sys.stdout:
        stream.close()
Ejemplo n.º 2
0
 def test_save(self, m_s):
     """Beautify a SeedsWorkbook and to a file."""
     swb = SeedsWorkbook()
     swb.save('file.xlsx')
     m_s.assert_called_with('file.xlsx')