使用python在excel文件中读写

时间:2017-07-07 13:27:32

标签: python excel xlrd xlwt

我正在尝试在python中读取excel文件的单元格,更改其值并保存它。如果我在代码中没有行from xlutils.copy import copy,则会识别workbook.sheeets(),但除此之外它会显示

AttributeError: 'Workbook' object has no attribute 'sheets'


from xlutils.copy import copy

import xlrd
st = xlwt.easyxf('pattern: pattern solid;')
rb = xlrd.open_workbook('00-17.xlsx')

workbook = copy(rb)
for sheet in workbook.sheets():
    for row in range(sheet.nrows):

        current_value = sheet.cell(row,3).value
        if(current_value == '1' ) :
            st.pattern.pattern_fore_colour = 5

            for column in [0,2,3]:
                value = sheet.cell(row, column).value

                print value
                sheet.write(row, column, value,st)
workbook.save('1.xlsx') 

1 个答案:

答案 0 :(得分:0)

我相信您可能必须先使用load_workbook()方法。查看链接Print Excel workbook using python,它可能会有所帮助。

您可以尝试使用workbook = xlrd.open_workbook('00-17.xlsx')并删除行workbook = copy(rb)吗?