Last active
August 20, 2023 09:38
-
-
Save nmz787/c43bc109db915064f188 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.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) |
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.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
You may want to incorporate the updates from my fork, to prevent
cels
from being overwritten every row, among other things. Or not. Let me try to fix that again, apparently I haven't fixed the issue.