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:
- Open workbook
- Iterate through cells for UNC path; as a hyperlink.
- 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\... - 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?