我有一個帶有11個工作表的.xlsx文件,我需要從第3行開始插入文本文件(製表符分隔,大約30列,100行)的內容。我嘗試了下面的代碼,但最終出現了錯誤。 (使用bash/Linux)的使用Python將文本文件插入現有的xlsx
#!/usr/bin/env python
import csv
from openpyxl.reader.excel import load_workbook
from xlrd import open_workbook
from xlutils import copy as xl_copy
with open('S12_final.txt') as tab_file: #open tab text file
tab_reader = csv.reader(tab_file, delimiter='\t')
xls_readable_book = load_workbook('S12.xlsx') #load workbook
xls_writeable_book = xl_copy.copy(xls_readable_book)
xls_writeable_sheet = xls_writeable_book.get_sheet_by_name('Filtered') #write data on this sheet
for row_index, row in enumerate(tab_reader):
xls_writeable_sheet.write(row_index, 0, row[0])
xls_writeable_sheet.write(row_index, 1, row[1])
xls_writeable_book.save('S12.xlsx') #save excel file
錯誤:
> Traceback (most recent call last): File "./tab2excel_a.py", line 23,
> in <module>
> xls_writeable_book = xl_copy.copy(xls_readable_book) File "/usr/local/lib/python2.7/dist-packages/xlutils-1.6.0-py2.7.egg/xlutils/copy.py",
> line 19, in copy
> w File "/usr/local/lib/python2.7/dist-packages/xlutils-1.6.0-py2.7.egg/xlutils/filter.py",
> line 937, in process
> reader(chain[0]) File "/usr/local/lib/python2.7/dist-packages/xlutils-1.6.0-py2.7.egg/xlutils/filter.py",
> line 61, in __call__
> filter.workbook(workbook,filename) File "/usr/local/lib/python2.7/dist-packages/xlutils-1.6.0-py2.7.egg/xlutils/filter.py",
> line 287, in workbook
> self.wtbook.dates_1904 = rdbook.datemode AttributeError: 'Workbook' object has no attribute 'datemode'
有什麼建議?
你好,試圖把另一堆錯誤'File「/usr/local/lib/python2.7/dist-packages/xlwt-0.7.5-py2.7.egg/xlwt/Row.py」,第42行, (65536)「%rowx) ValueError:行索引(65536)不在範圍內的整數(65536)' ' – nbn
第一行是什麼?__init__ raise ValueError(」行索引(%r)那個追溯? – armatita
'回溯(最近一次通話最後): 文件「./tab2excel_a.py」,第24行,在 xls_writeable_book = xl_copy.copy(xls_readable_book)'我的文件是一個xlsx文件,不知道這是否有所作爲 –
nbn