使用 python 将 txt 转换为 xlsx 时出错
Error in converting txt to xlsx using python
我的代码如下。
import csv
import openpyxl
import sys
def convert(input_path, output_path):
"""
Read a csv file (with no quoting), and save its contents in an excel file.
"""
wb = openpyxl.Workbook()
ws = wb.worksheets[0]
with open(input_path) as f:
reader = csv.reader(f, delimiter='\t', quoting=csv.QUOTE_NONE)
for row_index, row in enumerate(reader):
for 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()
但是我遇到了这个错误。
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
我想我已经正确安装了openpyxl。
而且我记得我以前使用这个程序没有任何问题。我最近买了一台新电脑,所以这可能是电脑配置问题。但我想不通。
根据 changelog for Openpyxl 2.0.0 -
,您似乎正在使用 openpyxl 2.0.0 +
Cells are referenced with 1-indexing: A1 == cell(row=1, column=1)
行和列索引从 1 开始。因此您也应该使 enumerate()
函数从 1 开始。例子-
for row_index, row in enumerate(reader, 1):
for col_index, value in enumerate(row, 1):
ws.cell(row=row_index, column=col_index).value = value
您的特定代码可以在低于 2.0.0 的 openpyxl 版本中运行,但由于上述 2.0.0 版本中的更改而失败。
我的代码如下。
import csv
import openpyxl
import sys
def convert(input_path, output_path):
"""
Read a csv file (with no quoting), and save its contents in an excel file.
"""
wb = openpyxl.Workbook()
ws = wb.worksheets[0]
with open(input_path) as f:
reader = csv.reader(f, delimiter='\t', quoting=csv.QUOTE_NONE)
for row_index, row in enumerate(reader):
for 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()
但是我遇到了这个错误。
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
我想我已经正确安装了openpyxl。
而且我记得我以前使用这个程序没有任何问题。我最近买了一台新电脑,所以这可能是电脑配置问题。但我想不通。
根据 changelog for Openpyxl 2.0.0 -
,您似乎正在使用 openpyxl 2.0.0 +Cells are referenced with 1-indexing: A1 == cell(row=1, column=1)
行和列索引从 1 开始。因此您也应该使 enumerate()
函数从 1 开始。例子-
for row_index, row in enumerate(reader, 1):
for col_index, value in enumerate(row, 1):
ws.cell(row=row_index, column=col_index).value = value
您的特定代码可以在低于 2.0.0 的 openpyxl 版本中运行,但由于上述 2.0.0 版本中的更改而失败。