我正在尝试构建一个报表生成器,它读取excel表并返回包含值的行。我建立了一个版本,我需要的,但只适用于csv这只是我的第一个代码混搭在一起,但它工作。我现在也希望包含条件格式(突出显示某些单元格值,例如if<;65格式为红色),因此需要我用xlsx表而不是csv重写。 下面是我试图让这个工作。。。 我可以找到值并返回行,但在第二次运行时返回一个错误
AttributeError: 'Worksheet' object has no attribute 'cell_value'
这是令人惊讶的,因为它刚刚起作用,并逐步通过代码返回我想要的值。。。。我尝试将其更改为.value,但返回:
AttributeError: 'function' object has no attribute '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的替代方案。 我没有您的数据,所以测试有点困难,但是任何语法错误都可以修复。请让我知道如果这不运行,我会帮助修复,如果需要。在
我假设你的输出是一个单独的文件(报告.xlsx这里)和每个选中工作簿的选项卡(每个选项卡都以源书名命名)。在
相关问题 更多 >
编程相关推荐