使用Python处理Excel表

时间:2014-12-25 16:47:05

标签: python excel

我想用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')
  • 日期单号项目金额结算业务员
  • 12.1 100001 A 100现金李雷
  • 12.1 100002 D 200现金韩梅梅
  • 12.1 100003 A 100现金李雷
  • 12.1 100004 E 450现金小明
  • 12.1 100005 R 430现金小明
  • 12.1 100006 A 100现金李雷
  • 12.1 100007 A 100现金李雷
  • 12.1 100008 A 100现金李雷
  • 12.1 100010 A 90现金韩梅梅
  • 12.1 100011 K 120现金小红

它无法正常工作。有什么不对?

例外:尝试覆盖单元格:sheetname = u'\ u674e \ u96f7'rowx = 2 colx = 0

1 个答案:

答案 0 :(得分:2)

您需要允许覆盖:

newsheet = newbook.add_sheet('李雷',cell_overwrite_ok=True)