I would like to use OpenPyXL to search the book, but I am facing some problems that I hope someone can help.
Here are a few / to -dos obstacles:
- I have an unknown number of sheets and cells
- I want to search a book and put the sheet names in an array
- I want to loop through each element of an array and look for cells containing a specific string
- I have cells with UNC paths that reference the old server. I need to extract all the text after the server name in the UNC path, update the server name and associate the remaining text with the server name
for example. \ File Server \ blah \ blah \ blah.xlsx; extract \ file-server \; replace with \ file-server1 \; put the remaining blah \ blah \ blah.xlsx after the new name. - Save xlsx document
I'm new to Python, so can someone point me in the right direction? Sample code is welcome, because all I know how to do at this point is to search for a known book with known sheet names, and then print the data. I don't know how to include wildcards when repeating through sheets and cells.
What I did to show the contents of the cells:
from openpyxl import load_workbook, worksheet def main(): #read workbook to get data wb = load_workbook(filename = 'Book1_test.xlsx', use_iterators = True) ws = wb.get_sheet_by_name(name = 'Sheet1') #ws = wb.worksheets #Iterate through worksheet and print cell contents for row in ws.iter_rows(): for cell in row: print cell.value #Iterate through workbook & print worksheets #for sheet in wb.worksheets: # print sheet if __name__ == '__main__': main()
----------------------- Update ----------------------- -
I can search through cells and retrieve the server name from the cell, but I cannot save the table because I am in read-only mode. When I try to switch to optimized_write = True, I get an error message:
AttributeError: the 'ReadOnlyCell' object does not have the 'upper' attribute
Here is my code:
from openpyxl import load_workbook, worksheet, Workbook def main():
Does anyone know how to update cell contents?
source share