在python中从Excel中查找并替换单元格 [英] Find and replace in cells from excel in python

查看:1303
本文介绍了在python中从Excel中查找并替换单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在.xlsx文件中具有"= ..."的单元格的位置,我想将"=替换为'=,因此可以将这些单元格视为字符串而不是值.

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.

例如

A1 = 5

A2 = 10

A3 = (A1/A2) = 0.5

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

I want to see =A1/A2 rather than 0.5.

在此先感谢您的帮助.

推荐答案

如建议 openpyxl 解决了这个问题:

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

这篇关于在python中从Excel中查找并替换单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆