Python Openpyxl-看不到价格
我正在编写一个程序,在工作簿中搜索零件号。如果它找到指定的零件号,它将遍历该行以找到您指定的零件价格值,并将两者交换为替换值 然而,我的代码没有找到价格Python Openpyxl-看不到价格,python,python-3.x,openpyxl,Python,Python 3.x,Openpyxl,我正在编写一个程序,在工作簿中搜索零件号。如果它找到指定的零件号,它将遍历该行以找到您指定的零件价格值,并将两者交换为替换值 然而,我的代码没有找到价格 import openpyxl import os from datetime import datetime DIRECTORY = os.path.dirname(os.path.realpath(__file__)) extentions = (".xlsx", ".xlsm", ".xltx", ".xltm") target =
import openpyxl
import os
from datetime import datetime
DIRECTORY = os.path.dirname(os.path.realpath(__file__))
extentions = (".xlsx", ".xlsm", ".xltx", ".xltm")
target = input("\033[1m\033[96mPart number: \033[0m")
target_replacement = input("\033[1m\033[96mReplace with: \033[0m")
price = input("\033[1m\033[96mPrice: \033[0m")
price_replacement = input("\033[1m\033[96mReplace with: \033[0m")
for (root, dirs, files) in os.walk(DIRECTORY):
for file in files:
if (file.endswith(extentions)):
path = os.path.join(root, file)
print(
"\033[1m\033[96mOpening:\033[0m \033[1m\033[93m{0}\033[0m".format(file))
wb = openpyxl.load_workbook(path, data_only=True)
ws = wb.active
target_in_wb = False
for ws in wb.worksheets:
for row in ws.iter_rows():
target_in_row = False
price_in_row = False
for cell in row:
if (cell.value == target):
print("\033[1m\033[92mTARGET STRING FOUND\033[0m")
print("\033[1m\033[96mReplacing\033[0m \033[1m\033[93m{0}\033[0m with \033[1m\033[93m{1}\033[0m on row \033[1m\033[93m{2}\033[0m".format(
target, target_replacement, ws._current_row))
cell.value = target_replacement
target_in_wb = True
for cell in row:
target_in_row = False
if (cell.value == price):
print(
"\033[1m\033[92mPRICE STRING FOUND\033[0m")
print("\033[1m\033[96mReplacing\033[0m \033[1m\033[93m{0}\033[0m with \033[1m\033[93m{1}\033[0m on row \033[1m\033[93m{2}\033[0m".format(
price, price_replacement, ws._current_row))
cell.value = price_replacement
price_in_row = True
if (target_in_row == False):
print(
"\033[1m\033[91mPrice string not found\033[0m")
if (target_in_wb == False):
print("\033[1m\033[91mPart not found\033[0m")
print(
"\033[1m\033[96mSaving:\033[0m \033[1m\033[93m{}\033[0m at \033[1m\033[96m{}\033[0m\n".format(file, datetime.now()))
wb.save(file)
print("\033[95m[\033[0m\033[96m*\033[0m\033[95m]\033[0m \033[1m\033[96mDone\033[0m")
我可以通过更改以下内容来解决此问题:
price = input("\033[1m\033[96mPrice: \033[0m")
price_replacement = input("\033[1m\033[96mReplace with: \033[0m")
致:
)您是否检查过使用相同的编码?单个单元格是否可以使用不同的编码?它可以识别包含文本字符串的单元格,但我的代码似乎无法识别带小数的数字。您是否尝试过使用已知匹配的特定大小写或单元格进行测试?试着打印出结果并进行比较。是的,我做了,它打印出了我提供的确切价格。我得到了它的工作,我会提供一个答案如下。
price = float(input("\033[1m\033[96mPrice: \033[0m"))
price_replacement = float(input("\033[1m\033[96mReplace with: \033[0m"))