I have a spread sheet where Column A
has a list of computer names with domain\\
preceding the computer name. I am trying to use openpyxl
to remove the domain\\
and leave just the computer names. Here is the code I have tried. There is no error however the script does not change anything on the spreadsheet.
import openpyxl
excelFile = openpyxl.load_workbook('C:\Users\user1\Documents\file.xlsx')
sheet1 = excelFile.get_sheet_by_name('Sheet1')
currentRow = 1
for eachRow in sheet1.iter_rows():
if sheet1.cell(row=currentRow, column=1).value == "'domain\'":
sheet1.cell(row=currentRow, column=1).value = ""
currentRow += 1
excelFile.save('C:\Users\user1\Documents\file.xlsx')
The easiest way is to replace the cell value with a trimmed string.
import openpyxl
filename = r'C:\Users\user1\Documents\file.xlsx'
excelFile = openpyxl.load_workbook(filename)
sheet1 = excelFile.active
for row in sheet1.iter_rows(min_col=1, max_col=1):
for cell in row:
if 'domain\\' in cell.value:
cell.value = cell.value[7:] #This will replace the cell value with a trimmed string
excelFile.save(filename)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.