Replace part of a cell value with blank with openpyxl - python

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)

Related

I want to iterate this list in my excel file, can you show me a different way to do it?

My question is simple and I'm sorry to ask it here. But I tried several ways to iterate through my excel file and I'm having trouble finding the solution.
from openpyxl import workbook, load_workbook
wb = load_workbook("italian_team.xlsx")
ws = wb.active
rows = ws["A"]
equipe = ["Juventus", "Ac Milan", "Torino", "Pescara", "As Roma", "Genoa", "Napoli"]
for cell in rows:
x = equipe[cell]
wb.save("italian_team.xlsx")
Do you mean you just want to insert your list as a row in the workbook?
If so there are a few options, you could just append the list as is to the sheet in which case it will be enter after the last used row.
Or specify the row (and column) to add to.
Both options are shown in the code below
from openpyxl import workbook, load_workbook
wb = load_workbook("italian_team.xlsx")
ws = wb.active
# rows = ws["A"]
equipe = ["Juventus", "Ac Milan", "Torino", "Pescara", "As Roma", "Genoa", "Napoli"]
# for cell in rows:
# x = equipe[cell]
# This will append the list after the last used row
ws.append(equipe)
# This will enter the list at row 1 column 1 to the length of the list
# Use min_row = and max_col = as well if the list is to be on another row or start at another column
for row in ws.iter_rows(max_row=1, max_col=len(equipe)):
for enum, cell in enumerate(row):
cell.value = equipe[enum]
wb.save("italian_team.xlsx")

Looping through each cell of a column in Excel using Python

I have an Excel file with data in column A. I want to loop through each cell and stop once i reach the first cell that has a formula.
This is my code:
wb = openpyxl.load_workbook(r"path\filename.xlsx")
sheet = wb['Sheet1']
names=sheet['A']
for cellObj in names:
if cellObj.value.str[0] == '=':
print(cellObj.value)
But i get an error...
The response from #John Giorgio worked for that part.
Now what i'm trying to do next is to select the entire row and paste special.
Here's my code:
import openpyxl
wb=openpyxl.load_workbook(path1)
sheet = wb['Sheet1']
names=sheet['A']
for cellObj in names:
val = str(cellObj.value)
if val[0] == "=":
#print(val)
excel.Range("A10:TM10").Select() #PART I WANT TO SELECT IF TRUE
excel.Selection.PasteSpecial(Paste=-4163)
So once I come across the first cell with '=' i want to select the entire row or at least from column A to TM and hardcode it / pasteSpecial
wb = openpyxl.load_workbook(r"path\filename.xlsx")
sheet = wb['Sheet1']
names=sheet['A']
for cellObj in names:
val = str(cellObj.value)
if val[0] == "=":
print(val)

Search specific text(text pattern) in excel and copy all resulting rows to another sheet in same workbook using openpyxl

I have an excel file with multiple sheets, 3rd column(contains around 500 rows) of sheet3 contains various names. I want to search column 3 for specific text and if it matches then copy the whole row along with the header row to new sheet within same excel.
Issue with "name column" is that most of the text refer to same item but naming convention is different, so:
अपर तहसीलदार,
नायाब तहसीलदार,
नायब तहसीलदार,
अतिरिक्त तहसीलदार
refers to same item but written differently, so for that I have to search for all variants.
I have no prior Python or openpyxl background so what I've got so far is:
import openpyxl
wb = openpyxl.load_workbook(r'C:/Users/Anas/Downloads/rcmspy.xlsx')
#active worksheet data
ws = wb.active
def wordfinder(searchString):
for i in range(1, ws.max_row + 1):
for j in range(1, ws.max_column + 1):
if searchString == ws.cell(i,j).value:
print("found")
print(ws.cell(i,j))
wordfinder("अपर तहसीलदार")
It is not showing any error but don't print anything either.
The excel sheet looks something like this:
I'm not certain, but I would suggest something along the lines of:
variants = {'alpha','alfa','elfa'}
data = []
rowCount = 0
for row in ws.values:
//each row is an array of cells
if rowCount == 0:
//header row
data.append(row)
elif row[2] in variants:
data.append(row)
rowCount += 1
wsNew = wb.create_sheet('Variations')
for line in data:
wsNew.append(line)
wb.save('newWorkbook.xlsx')

copy value to certain column in different sheet using openpyxl

I am trying to get the column value from worksheet1 to worksheets2(in specific column), while skipping all the nul/None value in between. My code worked when I printed out all the values in worksheet1 column, exluding all the nul values. However when I saved it to worksheet2, it only showed the last value and duplicate that to the whole column(from row 2 to 20).
Don't know why only last value was written in the new column
from openpyxl import Workbook
from openpyxl import load_workbook
source_file = (r'XXX(Source file).xlsx')
dest_file = (r'XXX(dest file).xlsx')
wb1=load_workbook(source_file, data_only=True)
wb1.active=0
ws1=wb1.active
wb2=load_workbook(dest_file)
wb2.active=0
ws2=wb2.active
for a in range(9,43):
cell2 = ws1.cell(row = a, column = 10)
if cell2.value is None or cell2.value == 0:
continue
else:
print(cell2.value)
for b in range(2,20):
ws2.cell(row = b, column=4).value = cell2.value
wb2.save(dest_file)
Your second loop is nested so that it will always overwrite all values in the column of the second sheet with the same value from the first.
I'd do something like this:
idx = 2
for row in ws1.iter_rows(min_row=9, max_row=43, min_col=10, max_col=10):
cell = row[0]
if not cell.value:
ws2.cell(row=idx, column=4, value=cell.value)
idx += 1

Starting iterate from specific column

This is my .xslx file:
data.xlsx
from openpyxl import load_workbook
workbook = load_workbook('/path/', read_only=True)
first_sheet = workbook.sheetnames()[0]
worksheet = workbook[first_sheet]
for row in worksheet:
for cell in row:
print(cell.value)
How can I start iteration from A4? I need to print year and month value.
You can use iter_rows, either by specifying the offset or the range
for row in worksheet.iter_rows('A4:B9'):
for cell in row:
print(cell.value)
Btw: sheetnames()[0] should be sheetnames[0] or get_sheet_names()[0]

Categories