我想用Python处理Excel表格,但有问题。
# -*- coding: utf-8 -*-
import xlrd
from xlwt import *
book = xlrd.open_workbook('myExcel.xls') #open a table
sheet = book.sheets()[0]
nrows = sheet.nrows
books = []
for i in range(nrows):
value = sheet.cell_value(i, 5)
if value == u'李雷':
ss = sheet.row_values(i)
for ii in range(len(ss)):
data = ss[ii] #get data in the specified row
books.append(data)
newbook = Workbook(encoding = 'utf-8') # open a new table
newsheet = newbook.add_sheet('李雷')
for item in books:
for item_row in range(2, 100):
for item_col in range(7):
newsheet.write(item_row, item_col, label = item) #write data
newbook.save('newExcel.xls')
它无法正常工作。有什么不对?
例外:尝试覆盖单元格:sheetname = u'\ u674e \ u96f7'rowx = 2 colx = 0
答案 0 :(得分:2)
您需要允许覆盖:
newsheet = newbook.add_sheet('李雷',cell_overwrite_ok=True)