spences10
5/15/2015 - 9:57 PM

git-xlsx-textconv-python

git-xlsx-textconv-python

import xlrd
import sys


if __name__ == '__main__':
    if len(sys.argv) != 2:
        print "Usage: git-xlsx-textconv file.xslx"
    
    excelFileName = sys.argv[1]

    xlFile = xlrd.open_workbook(excelFileName)
    if not xlFile:
        raise Exception('the file provided was not an readable Excel file.')

    worksheets = xlFile.sheet_names()
    for worksheet_name in worksheets:
        worksheet = xlFile.sheet_by_name(worksheet_name)
        for row_index in xrange(worksheet.nrows):
            row = worksheet.row(row_index)

            if not row:
                continue

            cels = []
            for cell_index in xrange(worksheet.ncols):
                s = str(worksheet.cell_value(row_index, cell_index))
                s = s.replace(r"\\", "\\\\")
                s = s.replace(r"\n", "\\n")
                s = s.replace(r"\r", "\\r")
                s = s.replace(r"\t", "\\t")
                cels.append(s)

            print "[{}]: {}\n".format(worksheet_name, '\t'.join(cels))
import xlrd
import sys

if __name__ == '__main__':
    if len(sys.argv) != 2:
        print "Usage: git-xlsx-textconv file.xslx"
    
    excelFileName = sys.argv[1]

    xlFile = xlrd.open_workbook(excelFileName)
    if not xlFile:
        raise Exception('the file provided was not an readable Excel file.')

    worksheets = xlFile.sheet_names()
    for worksheet_name in worksheets:
        worksheet = xlFile.sheet_by_name(worksheet_name)
        for row_index in xrange(worksheet.nrows):
            row = worksheet.row(row_index)

            if not row:
                continue
            cels = []
            row_as_string = "[{}] row {}:".format(worksheet_name, row_index+1)
            for cell_index in xrange(worksheet.ncols):
                s = str(worksheet.cell_value(row_index, cell_index))
                s = s.replace(r"\\", "\\\\")
                s = s.replace(r"\n", " ")
                s = s.replace(r"\r", " ")
                s = s.replace(r"\t", " ")
                if s:
                    cels.append('{}[col:{}] {}\n'.format(row_as_string, cell_index, s))
            if cels:
                print ''.join(cels)