-
-
Save icedwater/c1f1d729333cac6d2729 to your computer and use it in GitHub Desktop.
git-xlsx-textconv-python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.