Find and replace in cells from excel in python

前端 未结 1 1638
遥遥无期
遥遥无期 2021-01-17 05:38

Where I have a cell in an .xlsx file that is \"=...\" I want to replace the \"=\" with \'=, so can see the cells as strings rather than as the values.

For example,

1条回答
  •  囚心锁ツ
    2021-01-17 06:35

    As suggested openpyxl solves this problem:

    import openpyxl
    from openpyxl.utils.cell import get_column_letter
    
    wb = openpyxl.load_workbook('example.xlsx')
    wb.sheetnames
    sheet = wb["Sheet1"]
    amountOfRows = sheet.max_row
    amountOfColumns = sheet.max_column
    
    for i in range(amountOfColumns):
        for k in range(amountOfRows):
            cell = str(sheet[get_column_letter(i+1)+str(k+1)].value)
            if( str(cell[0]) == "="):
                newCell = "'=,"+cell[1:]
                sheet[get_column_letter(i+1)+str(k+1)]=newCell
    
    wb.save('example_copy.xlsx')
    

    0 讨论(0)
提交回复
热议问题