python去掉excel空格和换行,报错'int' object has no attribute 'strip'

在网上找了一段代码,进行运行后运行错误,哪位朋友帮忙看看

    #去除exce空格和空行
    def clear_blank_space(self):
        #加载文件
        wb=load_workbook(self.file_name)
        ws=wb.active
        for i in range(1,ws.max_row+1):
            for j in range(1,ws.max_column+1):
                old=ws.cell(i,j).value
                if old is not None:
                    ws.cell(i, j).value = old.strip().replace(' ', '').replace("\n", "")
        wb.save(self.file_name)
        wb.close()
        print('处理完成')

运行结果报错:

D:\Testing_Software\Python38\python.exe D:/Code/PycharmProjects/test_rigang/utile/operation_excel.py
Traceback (most recent call last):
File “D:/Code/PycharmProjects/test_rigang/utile/operation_excel.py”, line 87, in
opers.clear_blank_space()
File “D:/Code/PycharmProjects/test_rigang/utile/operation_excel.py”, line 80, in clear_blank_space
ws.cell(i, j).value = old.strip().replace(‘ ‘, ‘’).replace(“\n”, “”)
AttributeError: ‘int’ object has no attribute ‘strip’

本作品采用《CC 协议》,转载必须注明作者和本文链接
讨论数量: 3
Jason990420

You asked this question in wrong place.

发布问答

My answer for your question is

The data stored in your excel file is in integer, so you got integer variable from ws.cell(i,j).value, not str. That's why you got error message: ‘int’ object has no attribute ‘strip’.

3年前 评论
cherish_sunflower (楼主) 3年前
PaCent 1年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!