Error in converting txt to xlsx using python -
my code following.
import csv import openpyxl import sys def convert(input_path, output_path): """ read csv file (with no quoting), , save contents in excel file. """ wb = openpyxl.workbook() ws = wb.worksheets[0] open(input_path) f: reader = csv.reader(f, delimiter='\t', quoting=csv.quote_none) row_index, row in enumerate(reader): col_index, value in enumerate(row): ws.cell(row=row_index, column=col_index).value = value wb.save(output_path) def main(): try: input_path, output_path = sys.argv[1:] except valueerror: print 'usage: python %s input_path output_path' % (sys.argv[0],) else: convert(input_path, output_path) if __name__ == '__main__': main()
but got error.
traceback (most recent call last): file "txt2xlsx.py", line 33, in <module> main() file "txt2xlsx.py", line 29, in main convert(input_path, output_path) file "txt2xlsx.py", line 18, in convert ws.cell(row=row_index, column=col_index).value = value file "c:\python27\lib\site-packages\openpyxl\worksheet\worksheet.py", line 350, in cell column = get_column_letter(column) file "c:\python27\lib\site-packages\openpyxl\utils\__init__.py", line 100, in get_column_letter raise valueerror("invalid column index {0}".format(idx)) valueerror: invalid column index 0
i think have installed openpyxl correctly.
and remember using program without problem before. bought new computer maybe pc configuration issue.. can't figure out.
seems using openpyxl 2.0.0 + ,according changelog openpyxl 2.0.0 -
cells referenced 1-indexing: a1 == cell(row=1, column=1)
the rows , columns index start @ 1. should make enumerate()
function start @ 1 well. example -
row_index, row in enumerate(reader, 1): col_index, value in enumerate(row, 1): ws.cell(row=row_index, column=col_index).value = value
your particular code work in openpyxl version less 2.0.0 , fails because of above mentioned change in version 2.0.0 .
Comments
Post a Comment