Cerca e Sostituisci Dati in un Intervallo
Possibili Scenari di Utilizzo
A volte è necessario cercare e sostituire dati specifici in un intervallo ignorando i valori delle celle al di fuori dell’intervallo desiderato. Aspose.Cells for Python via .NET ti consente di limitare una ricerca a un intervallo specifico. Questo articolo spiega come cercare e sostituire dati in un intervallo in Excel usando il codice Python.
Come Cercare e Sostituire Dati in un Intervallo usando Aspose.Cells per la Libreria Excel Python
Aspose.Cells per Python via .NET fornisce il metodo FindOptions.set_range() per specificare un intervallo durante la ricerca dei dati. Il campione di codice sottostante cerca e sostituisce i dati in un intervallo.
from aspose.cells import CellArea, FindOptions, LookAtType, LookInType, Workbook | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# The path to the documents directory. | |
dataDir = RunExamples.GetDataDir(".") | |
filePath = dataDir + "input.xlsx" | |
workbook = Workbook(filePath) | |
worksheet = workbook.worksheets[0] | |
# Specify the range where you want to search | |
# Here the range is E3:H6 | |
area = CellArea.create_cell_area("E9", "H15") | |
# Specify Find options | |
opts = FindOptions() | |
opts.look_in_type = LookInType.VALUES | |
opts.look_at_type = LookAtType.ENTIRE_CONTENT | |
opts.set_range(area) | |
cell = None | |
while True: | |
cell = worksheet.cells.find("search", cell, opts) | |
if cell: | |
cell.put_value("replace") | |
else: | |
break | |
# Save the workbook | |
workbook.save(dataDir + "output.out.xlsx") |