Skip to content

Instantly share code, notes, and snippets.

@icedwater
Forked from nmz787/each_cell_on_its_own_line
Last active March 2, 2021 18:57
Show Gist options
  • Save icedwater/c1f1d729333cac6d2729 to your computer and use it in GitHub Desktop.
Save icedwater/c1f1d729333cac6d2729 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.xlsx"
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)
cells = []
for row_index in xrange(worksheet.nrows):
row = worksheet.row(row_index)
if not row:
continue
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:
cells.append('{}[col:{}] {}\n'.format(row_as_string, cell_index, s))
if cells:
print ''.join(cells)
import xlrd
import sys
if __name__ == '__main__':
if len(sys.argv) != 2:
print "Usage: git-xlsx-textconv file.xlsx"
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)
cells = []
for row_index in xrange(worksheet.nrows):
row = worksheet.row(row_index)
if not row:
continue
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")
cells.append(s)
print "[{}]: {}\n".format(worksheet_name, '\t'.join(cells))
@icedwater
Copy link
Author

Shifted cells = [] into the worksheet area; it was emptying every row before. Also capitalised Exception message and fixed spelling error with extension in usage message.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment