【问题标题】:Find and replace in cells from excel in python在python中的excel中查找和替换单元格
【发布时间】:2019-02-15 23:36:38
【问题描述】:

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

例如,

A1 = 5

A2 = 10

A3 = (A1/A2) = 0.5

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

提前感谢您的任何帮助。

【问题讨论】:

标签: python excel python-3.x


【解决方案1】:

建议openpyxl 解决了这个问题:

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')

【讨论】:

    猜你喜欢
    • 2023-01-31
    • 2016-08-24
    • 2016-10-27
    • 2012-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-04
    • 1970-01-01
    相关资源
    最近更新 更多