xlsx写入cell_value错误,写入新的工作表

我试图build立一个报表生成器读取Excel表并返回包含值的行。 我build立了一个我需要的版本,但只适用于csv这只是我的第一个代码混搭在一起,但它的工作。 我现在也想包括条件格式(突出显示某些单元格的值,例如,如果<65格式红色),所以要求我用xlsx表格而不是csv重写。 下面是我试图得到这个工作…我可以find值并返回行,但在第二次运行通过它返回一个错误

AttributeError:“工作表”对象没有属性“cell_value”

这是令人惊讶的,因为它刚刚工作,并通过代码逐步回退我想要的值….我已经尝试将其更改为.value,但返回:

AttributeError:'function'对象没有属性'value'

帮助,我不知道我现在在做什么。 如果它没有任何意义,我很高兴发布我的原始代码为csv'解释'

谢谢

import xlsxwriter import xlrd import os import xlwt # open original excelbook and access first sheet for excelDocs in os.listdir('.'): if not excelDocs.endswith('.xlsx'): continue # skip non-xlsx files workbook = xlrd.open_workbook(excelDocs) sheet = workbook.sheet_by_index(0) cellslist = [] i = 0 #########WORKS!##################### for row in range(sheet.nrows): for col in range(sheet.ncols): if sheet.cell_value(row, col) == 'CP' or sheet.cell_value(row, col) == 'LNA' or sheet.cell_value(row, col) == 'Last Name': i = i + 1 data = [sheet.cell_value(0, col) for col in range(sheet.ncols)] workbook = xlsxwriter.Workbook() sheet = workbook.add_worksheet('excelDocs') for index, value in enumerate(data): sheet.write(i, index, value) workbook = xlrd.open_workbook(excelDocs) 

我没有使用xlsxwriter,xlrd或xlwt的经验。 因为这是你的“第一代码混搭”,我想我会提供一个使用openpyxl的替代scheme。 我没有你的数据,所以testing有点困难,但是任何语法错误都可以修复。 请让我知道如果这不运行,我会帮助修复,如果需要的话。

我假设你的输出是一个单独的文件(report.xlsx这里)和每个工作簿检查选项卡(每个选项卡命名为源书名)。

  import openpyxl from openpyxl import * from openpyxl.utils import get_column_letter interestingValues = ['CP','LNA', 'LastName'] report = Workbook() dest_filename = 'report.xlsx' # open original excelbook and access first sheet for excelDocs in os.listdir('.'): if not excelDocs.endswith('.xlsx'): continue # skip non-xlsx files workbook = load_workbook(excelDocs) sheet = workbook.active workingReportSheet = report.create_sheet(str(excelDocs.split('.')[0])) i = 0 for row in range(1,sheet.max_row): for col in range(sheet.max_column): columnLetter = get_column_letter(col +1) if str(sheet['%s%s' % (columnLetter,row)].value) in interestingValues: i += 1 data = [sheet['%s%s' % (str(get_column_letter(col)),i)].value for col in range(1,sheet.max_column +1)] for index, value in enumerate(data): workingReportSheet['%s%s' % (str(get_column_letter(index+1)),i)].value = value report.save(filename = dest_filename) 

再次读取您的代码,可能是您丢弃了您的输出。 试试下面。

 import xlsxwriter import xlrd import os import xlwt #Create output sheet outputworkbook = xlsxwriter.Workbook() # open original excelbook and access first sheet for excelDocs in os.listdir('.'): if not excelDocs.endswith('.xlsx'): continue # skip non-xlsx files workbook = xlrd.open_workbook(excelDocs) sheet = workbook.sheet_by_index(0) cellslist = [] i = 0 outputsheet = outputworkbook.add_worksheet('excelDocs') for row in range(sheet.nrows): for col in range(sheet.ncols): if sheet.cell_value(row, col) == 'CP' or sheet.cell_value(row, col) == 'LNA' or sheet.cell_value(row, col) == 'Last Name': i = i + 1 data = [sheet.cell_value(0, col) for col in range(sheet.ncols)] for index, value in enumerate(data): outputsheet.write(i, index, value)