from openpyxl import load_workbook
def search_cell_value(workbook_path, sheet_name, target_value):
# Load an excel file
wb = load_workbook(filename=workbook_path)
# select the sheet
sheet = wb[sheet_name]
# Initialize dictionary to store results
result = {'value': target_value, 'count': 0, 'positions': []}
# Search for all cells in the sheet
for row in range(1, sheet.max_row + 1):
for column in range(1, sheet.max_column + 1):
cell = sheet.cell(row=row, column=column)
# Verify that the value of the cell matches the target value
if cell.value == target_value:
# In case of matching value, records searched count and position information
result['count'] += 1
result['positions'].append((row, column))
return result
# You must change the physical path before running this script.
currPath = "C:/Users/natio/OneDrive/0. Personal Blog/05. Python/05. OPENPYXL/02. Cell Example/"
workbook_path = currPath+"Find Cell Value Example File.xlsx"
# input sheet name and searching string
sheet_name = 'Sample Sheet'
target_value = '!'
result = search_cell_value(workbook_path, sheet_name, target_value)
print(f"검색한 값: {result['value']}")
print(f"총 개수: {result['count']}")
print("위치 정보:")
for position in result['positions']:
print(f"행: {position[0]}, 열: {position[1]}")
from openpyxl import load_workbook
def search_cell_value(workbook_path, sheet_name, target_value):
# Load an excel file
wb = load_workbook(filename=workbook_path)
# select the sheet
sheet = wb[sheet_name]
# Initialize dictionary to store results
result = {'value': target_value, 'count': 0, 'positions': []}
# Search for all cells in the sheet
for row in range(1, sheet.max_row + 1):
for column in range(1, sheet.max_column + 1):
cell = sheet.cell(row=row, column=column)
# Verify that the value of the cell matches the target value
if cell.value == target_value:
# In case of matching value, records searched count and position information
result['count'] += 1
result['positions'].append((row, column))
return result
def replace_cell_value(workbook_path, sheet_name, target_value, replacement, new_workbook_path):
# Load an excel file
wb = load_workbook(filename=workbook_path)
# select the sheet
sheet = wb[sheet_name]
# Search for all cells in the sheet
for row in range(1, sheet.max_row + 1):
for column in range(1, sheet.max_column + 1):
cell = sheet.cell(row=row, column=column)
# Verify that the value of the cell matches the target value
if cell.value == target_value:
# Replace if value is matched
cell.value = replacement
# Save changes
wb.save(new_workbook_path)
# You must change the physical path before running this script.
currPath = "C:/Users/natio/OneDrive/0. Personal Blog/05. Python/05. OPENPYXL/02. Cell Example/"
workbook_path = currPath+"Find Cell Value Example File.xlsx"
# Set input values
sheet_name = 'Sample Sheet'
target_value = '!'
replacement = '@'
new_workbook_path = currPath+"Replaced Cell Value Example File.xlsx"
# Replace searching value
replace_cell_value(workbook_path, sheet_name, target_value, replacement, new_workbook_path)
# Result
result = search_cell_value(workbook_path, sheet_name, target_value)
print(f"검색 문자 : {result['value']}, 대체 문자 : {replacement}")
print(f"총 개수: {result['count']}")
print("위치 정보:")
for position in result['positions']:
print(f"행: {position[0]}, 열: {position[1]}")
▪결과 값
검색 문자 : !, 대체 문자 : @ 총 개수: 4 위치 정보: 행: 5, 열: 1 행: 10, 열: 1 행: 15, 열: 1 행: 20, 열: 1