Get excel cell address in Python 2 using openpyxl - python

I have the following scenario:
Read the excel file of column A for all active rows
Run if else statement: if column A == 'registered', do nothing and just read the next row. Else, get cell address (not value) and do some actions
For example, here is my "sample.xlsx":
[row 1] A1=registered B1=user1
[row 2] A2=registered B2=user2
[row 3] A3=null B3=user3
my code should ignore row1 and row2 and proceed with row 3. In row 3, I should get "A3" and do some actions.
from openpyxl import load_workbook
book = load_workbook(filename='sample.xlsx')
sheet = book.active
first_column = sheet['A']
for x in xrange(len(first_column)):
status = first_column[x].value
if status == 'registered':
#enter code to just proceed with the next row
else:
#enter code to get the column cell range and do some actions
book.save('sample.xlsx')
I got stucked with the scenario 2, I am a newbie in python, I have a code in java using FileScanner but I need it in Python 2. Appreciate your help.
UPDATE:
I already have an answer below but just wondering if there are any other method??

Have a look
import openpyxl
wb = openpyxl.load_workbook('sample.xlsx')
ws = wb.active
for row in ws.iter_rows('A{}:A{}'.format(ws.min_row,ws.max_row)):
for cell in row:
if cell.value != "registered":
print cell.column
print cell.row
## further processing here #######
No need to write logic for explicit row iteration this for loop will take care of that.
Hope it will help you :)

so far, I have tried these. is there any other method?
from openpyxl import load_workbook
book = load_workbook(filename='sample.xlsx')
sheet = book.active
first_column = sheet['A']
checker = 'false'
for x in xrange(len(first_column)):
status = first_column[x].value
xaddress = first_column[x].column
yaddress = first_column[x].row
celladdress = xaddress+str(yaddress)
if status != 'registered':
print celladdress
checker == 'true'
break
if checker == 'true':
#... my actions here ....
Here is my "sample.xlsx":
[row 1] A1=registered B1=user1
[row 2] A2=registered B2=user2
[row 3] A3=null B3=user3
Output: A3

It always helps to read the documentation
for cell in ws['A']:
pass

Related

Not able to export data into Excel using xlsxwriter library

I'm trying to iterate a For Loop such that the elements in the two lists get exported to excel in columns A and B. However, whenever I run the code it only displays a single number in column B row 1 (B1).
The entire code is too long so I'm attaching just a snippet of the code where I am stuck.
This is what I'm getting in my excel file when I run the code
#Exporting data to Excel
workbook = xlsxwriter.Workbook('efficient_front.xlsx')
worksheet = workbook.add_worksheet()
i = 1
if company == first_company:
for perc_return in returns:
worksheet.write('A' + str(i) , perc_return)
i =+ 1
else:
for perc_return in returns:
worksheet.write('B' + str(i), perc_return)
i =+ 1
workbook.close()
consider the given lists => prod_codes, ID_codes. The below code will write each list as a column in an excel sheet. The parameters of worksheet.write() are as shown below
worksheet.write(row_number,column_number,value_to_be_written)
prod_codes = [1001,1002,1003,1004,1005,1006]
ID_codes = [123,345,567,789,908,345]
with xlsxwriter.Workbook('PATH for XLSX to be created') as workbook:
worksheet = workbook.add_worksheet("NAME_ME")
for index,value in enumerate(ID_codes):
worksheet.write(index,0,value)
for index,value in enumerate(prod_codes):
worksheet.write(index,1,value)
Please go through the official documentation, it's clear how to perform what you need to perform. https://xlsxwriter.readthedocs.io/working_with_data.html
You have a silent syntax error in your code with i =+ 1 instead of i += 1. The code translates to i = +1 which is equivalent to i = 1 so it doesn't iterate.
Here is an alternative way to structure you code with enumerate() and the (row, col) syntax of worksheet.write():
import xlsxwriter
workbook = xlsxwriter.Workbook('efficient_front.xlsx')
worksheet = workbook.add_worksheet()
returns = [1, 2, 3, 4, 5]
company = True
first_company = False
if company == first_company:
col_num = 0
else:
col_num = 1
for row_num, perc_return in enumerate(returns):
worksheet.write(row_num, col_num, perc_return)
workbook.close()
Output:

Check for empty excel cell (Python)

I understand there are similar questions but I don't quite understand their answers. Any help is appreciated.
import xlrd
path = ('E:\clean.xlsx')
wb = xlrd.open_workbook(path)
sheet = wb.sheet_by_index(0)
is_empty = None
if (sheet.cell(row=0, column=0).value) == None:
print("empty")
How do I check for a specific cell in my excel file and check if it is empty? Thanks.
You can check the value using sheet.cell_value(rowx=row, colx=col). However the sheet is an array. As a result the code will error if the value is null. Therefore you'll need to check the indexes are inside the array using sheet.nrows and sheet.ncols
I'm sure theres better way of checking null or empty. However the following should work: if sheet.nrows > row and sheet.ncols > col and sheet.cell_value(rowx=row, colx=col):
Example:
import xlrd
path = ('E:\clean.xlsx')
wb = xlrd.open_workbook(path)
sheet = wb.sheet_by_index(0)
def is_null_or_empty(sheet, row, col):
return True if sheet.nrows > row and sheet.ncols > col and sheet.cell_value(rowx=row, colx=col) else False
print(f'has value for row {0} col {0}: {is_null_or_empty(sheet, 0, 0)}')
print(f'has value for row {0} col {1}: {is_null_or_empty(sheet, 0, 1)}')
Output:
has value for row 0 col 0: False
has value for row 0 col 1: True

Writing new excel file without data from previous workbook

I am trying to write a programm to compare strings from a fixed matrix to 2 specific columns from an excel file. So far, I am first trying to achieve that a comparison with a match in row takes place. So far, the comparison of one string from the matrix is successful.
import openpyxl as xl
from IDM import idm_matrix
wb = xl.load_workbook('Auswertung_C33.xlsx')
sheet = wb['TriCad_Format']
for row in range(2, sheet.max_row + 1):
cell = sheet.cell(row, 8)
if idm_matrix[0][0] in cell.value:
sheet.cell(row=2, column=1).value = cell.value
wb.save('Auswertung.xlsx')
Question: How can I achieve that the matching values are saved in a new file WITHOUT the loaded workbook above?
For further help with this project I will get back to you as soon as I am facing more difficulties with the matrix comparison.
Thanks for your help.
Regards, Alex
You will need to create a new workbook to save your answer (comparison result). something like below. Hope this is helpful.
import openpyxl as xl
from IDM import idm_matrix
wb = xl.load_workbook('Auswertung_C33.xlsx')
result_wb = xl.Workbook() #workbook to save your result.
result_sheet = result_wb.active #get the active sheet to save your result.
sheet = wb['TriCad_Format']
for row in range(2, sheet.max_row + 1):
row_list = []
for col in range(1, sheet.max_col+1):
cell = sheet.cell(row, col)
row_list.append(cell)
#adjust row,col offset to match your matrix index below, e.g. row-2, col-1. you might need another loop to loop through your matrix.
if idm_matrix[i][j] in row_list:
result_sheet.append(row_list)
result_wb.save('Auswertung.xlsx') #save the result workbook
#henjiFire: Thats how the code looks like right now:
for row in range(2, sheet.max_row + 1):
row_list = []
for col in range(1, sheet.max_column + 1):
cell = sheet.cell(row, col)
row_list.append(cell.value)
# adjust row,col offset to match your matrix index below, e.g. row-2, col-1. you might need another loop to loop through your matrix.
if idm_matrix[0][0] in row_list:
if row_list[14] is not None and idm_matrix[0][1] in row_list[14]:
result_sheet.append(row_list)

How to copy data from one sheet to another while skipping empty cells - Python and Openpyxl

I'm using python to format an Excel spreadsheet. I need to copy data from Column L in Sheet #1, "Main", and paste it into Column A in Sheet #2, "Data". I've gotten this working, but I also want to skip empty cells, which occur randomly in Sheet #1, and here I ran intro trouble.
I tried:
for i in range(2, 50):
for j in range(12, 13):
if cell.value != None:
data.cell(row=i, column=j-11).value = main.cell(row=i, column=j).value
However I get the error message "NameError: name 'value' is not defined"
Any ideas?
This is the code we got working (see the comments for the back and forth):
import os
import openpyxl
wb = openpyxl.load_workbook('/Users/path/.xlsx')
main = wb['Sheet1']
wb.create_sheet(title='Formatted Data')
data = wb['Formatted Data']
for i in range(2, 50):
for j in range(12, 13):
if main.cell(i,j).value != None:
data.cell(data.max_row+1, column=j-11).value = main.cell(row=i, column=j).value
Wherever possible you should avoid using your own counters and let openpyxl do the work for you. For a new worksheet this is pretty easy.
empty_row = [None] * 11
for row in main.iter_rows(min_col=12, max_col=2, min_row=2, values_only=True):
if row[0] != None:
data.append(empty_row + row]

Python-Excel: How to write lines of a row to an existing excel file?

I have searched the site but I could not find anything related to the following question.
I have an existing spreadsheet that I am going to pull data from on a daily basis, the information in the spreadsheet will change everyday.
What I want to do is create a file that tracks certain information from this cell, I want it to pull the data from the spreadsheet and write it to another spreadsheet. The adding of the data to a new spreadsheet should not overwrite the existing data.I would really appreciate the help on this. See code below:
import os
import openpyxl
import xlrd
wb=openpyxl.load_workbook('Test_shorts.xlsx','r')
sheet = wb.active
rows = sheet.max_row
col = sheet.max_column
rows = rows+1
print rows
new =[]
for x in range (2, 3):
for y in range(1,10):
z= sheet.cell(row=x,column=y).value
new.append(z)
print(new)
If you want to copy the whole worksheet, you can use copy_worksheet() function directly. It will create a copy of your active worksheet.
I don't know your data, but I am sure you can finish it by yourself. Hope this may help
from openpyxl import load_workbook
file_name = "Test_shorts.xlsx"
wb = load_workbook(file_name)
sheet = wb.active
target = wb.copy_worksheet(sheet)
# you can code to append new data here
new = wb.get_sheet_by_name(target.title) # to get copied sheet
for x in range (2, 3):
for y in range(1,10):
print(x,y)
z= sheet.cell(row=x,column=y).value
new.append(z)
wb.save(file_name)
as commented, a loop of cells are required so I altered your code a little.
from openpyxl import load_workbook
file_name = "Test_shorts.xlsx"
wb = load_workbook(file_name)
current_sheet = wb.active
new_sheet = wb.create_sheet("New", 1)
for row in current_sheet.rows:
col = 0 # set the column to 0 when 1 row ends
for cell in row:
col += 1 # cell.column will return 'ABC's so I defined col for the column
new_sheet.cell(cell.row, col, cell.value)
wb.save(file_name)

Categories