Skip to content

Instantly share code, notes, and snippets.

@jpi-seb
Forked from nmz787/each_cell_on_its_own_line
Created April 3, 2019 09:34
Show Gist options
  • Save jpi-seb/d7e9dc5c9be34281dd3714193f6fef63 to your computer and use it in GitHub Desktop.
Save jpi-seb/d7e9dc5c9be34281dd3714193f6fef63 to your computer and use it in GitHub Desktop.
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 = []
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)
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))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment