xlsx写入单元格值错误,写入新工作区

2024-09-30 22:15:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试构建一个报表生成器,它读取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)

Tags: csv代码inimportforifvaluecell
2条回答

再次读取代码时,可能是您放弃了输出。 试试下面的方法。在

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)

我没有使用xlsxwriter、xlrd或xlwt的经验。由于这是您的“第一次代码聚合”,我想我会提供一个使用openpyxl的替代方案。 我没有您的数据,所以测试有点困难,但是任何语法错误都可以修复。请让我知道如果这不运行,我会帮助修复,如果需要。在

我假设你的输出是一个单独的文件(报告.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)

相关问题 更多 >