Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用Python处理Excel表格_Python_Excel - Fatal编程技术网

用Python处理Excel表格

用Python处理Excel表格,python,excel,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

我想用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

您需要允许覆盖:

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

否则,每个单元格只允许一次写入。谢谢。出了点问题。我想要所有关于“李雷" 对于数据,并非所有单元格都是'李雷'.@辜,对不起,我不太理解你的评论,这个错误是关于
“的。”李雷“
,是否有其他错误?@padraiccnningham感谢您的回复。在我修改后运行脚本,所有单元格都将被删除”李雷'. 我不知道为什么?这不是我想要的。在上半部分,“数据”显然是“数据”的行李雷', 写一个Excel,它变成了'李雷'