在Python中的Excel单元格中查找和替换

2023-12-26

如果 .xlsx 文件中有一个单元格为“=...”,我想将“=”替换为“=”,因此可以将单元格视为字符串而不是值。

例如,

A1 = 5

A2 = 10

A3 = (A1/A2) = 0.5

我想看看=A1/A2而不是 0.5。

预先感谢您提供的任何和所有帮助。


按照建议openpyxl https://openpyxl.readthedocs.io/en/stable/解决这个问题:

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')
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在Python中的Excel单元格中查找和替换 的相关文章