python去掉excel空格和換行,報錯'int' object has no attribute 'strip'

cherish_sunflower發表於2020-07-25

在網上找了一段程式碼,進行執行後執行錯誤,哪位朋友幫忙看看

    #去除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 協議》,轉載必須註明作者和本文連結

相關文章