OpenPyXL + How can I search for content in a cell in Excel, and if the content matches the search criteria update the content?

Question:

I’m working on a Python (using 2.7) project to search through excel files for a UNC path for a server that is changing, and then update the cell with the new UNC path. I’m new to python and I’m able to find the cell and print it with:

def main():
    wb = load_workbook(filename = 'Book1_test.xlsx', use_iterators = True)
    ws = wb.get_sheet_by_name(name = 'Sheet1')

    for row in ws.iter_rows(): 
        for cell in row:
            print cell.value

But, I don’t know how to update the cell with the new string, and the workbook appears to be in read only mode; probably because ws is only retrieving information.
I found lots of resources online for searching cells and printing information but not on how to update the cell after the information is found. There is a lot of information on how to create a new workbook with blanks cells, and then update your own information; also, on how to open an existing worksheet and insert contents into a new cell. I’m just not finding what I’m looking for and my skills aren’t there yet to do it myself.

Here are my goals, and if someone can help I’d greatly appreciate it:

  1. Open workbook
  2. Iterate through cells for UNC path; as a hyperlink.
  3. Write new UNC path/formula
    Note: I only want to update the first part of the link referencing the server name. For example, file:///\enterprise… is to be file:///\file-server…
  4. Close workbook

Shortcomings I’m hoping someone can help me with:

  • I don’t understand for loop syntax enough to iterate through cells for non-specific content; meaning I don’t know how to search for just a word within a string.
  • Should I worry about capturing cell numbers/coordinates?
  • Should this problem be approached by iterating cells, storing contents to variable, parsing string and extract just the content I’m looking for(server name), concatenate the other-info before and after the new server name, and then update the cell contents?
Asked By: Tellander

||

Answers:

The .value property of the Cell object is settable. This means that you can do any time something like:

cell.value = 'Helloooo ladies'

If this does not work it is probably because of the optimizer read mode. Check the optimized write mode if you needed… I always found the docs of openpyxl very complete and easy to follow.

How to look for information in the cell, or recognize what you are looking for is a very open question that can be solved in thousand different ways:

  • Checking the full content of the cell
  • Checking that a given substring is contained in the cell
  • Matching with a regular expression

But this does not have anything to do with the openpyxl framework.

Answered By: bgusach

You can search for a content by iterating within two cell ranges

for rowOfCellObjects in sheet['A1':'J15']: # You can mention the range here like A1 to A50
    for cellObj in 'rowOfCellObjects':
        print(cellObj.value)
        print(cellObj.coordinate)

Updating can be done by mentioning cell name

ws['A1'] = 1
Answered By: Mounesh
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.