I want to read a row from an excel sheet based on the user input. My excel sheet contains columns with number, priority, assignee and other details.
Eg:- if I want to fetch the entire row of data for number= PRB00000, I am currently doing that with the below code. I am using a list and manually finding the index of it and hard-coding it to a VARIABLE and then sending it to selenium webdriver eg:- webelement.send_keys(variable). I don't want to do that and instead read the row based on user input and store the values in respective columns names.
book=xlrd.open_workbook('C:\\xxxx\\xxxx\\third party power.xlsx')
#print (book.nsheets)
#print (book.sheet_names())
first_sheet=book.sheet_by_index(0)
prb= "PRB0045087"
list1 = []
for sheet in book.sheets():
for rowidx in range(sheet.nrows):
row=sheet.row(rowidx)
for colidx, cell in enumerate(row):
if cell.value == prb :
list1 = first_sheet.row_values(rowidx)
RCA = list1[20]
LTF = list1[21]
Suggest you do a dictionary of IDs and row index, that way you can look it up
ids = {cell.value:colidx for colidx,cell in enumerate(row)}
Related
I am having a hard time trying to find anything relating to my question. All I have found so far is selecting ranges based off of a static range, but unfortunately the data can change from week to week.
There are multiple data blocks with different rows and columns located in the same sheet but have titles above the data. My goal is to find a title i.e. row 36 or 40, move a row down and essentially do a ctrl+down ctrl+right for selecting a range and then creating a table and naming a table based off of the title.
import openpyxl
def tables(title):
for cell in pws_sheet["A"]: #pws_sheet["A"] will return all cells on the A column until the last one
if (cell.value is not None): #check if cell is not empty
if title in cell.value: #check if the value of the cell contains the title
row_coord = cell.row #put row number into a variable
tables("All Call Distribution by Hour")
I'm currently able to find the row based off of the title, save the title into a variable, but I am lost on figuring out how to select the bottom right of each data block and selecting it as a range and creating the table from that range.
EDIT 1: Title row is correct, end row is the acting like max_row, and the num_cols is showing the cell.values instead of just a single max column for that table.
def find_table(title, sheet):
title_row = None
for row in sheet.iter_rows():
if row[0].value == title:
#Find the title row
title_row = row[0].row
if row[0].value is None and title_row:
end_row = row[0].row - 1
num_cols = [cell.value for cell in sheet[title_row+1] if cell.value is not None]
else:
#The last row in the sheet
end_row = row[0].row
print(f"Row: {title_row}, Column: {num_cols}, End Row: {end_row}")
return title_row, num_cols, end_row
OUTPUTS: Row: 40, Column: ['Within', '# Calls', '% Calls'], End Row: 138
For selecting the cells you want, try something like this
def find_table(sheet, title):
title_row = None
for row in sheet.iter_rows():
if row[0].value == title:
# Find the title row
title_row = row[0].row
if row[0].value is None and title_row:
end_row = row[0].row - 1
break
else:
# The last row in the sheet
end_row = row[0].row
return title_row, end_row
You can find the specific number of columns, for the given table with;
num_cols = len([cell.value for cell in sheet[title_row+1] if cell.value is not None])
That should give you the start and end rows, and the number of columns. You can then select those cells and use them to "make a table" in whatever form that takes for your specific example.
If you want to select a range of cells using Excels 'A1' style notation, you can always use openpyxl.utils.cell.get_column_letter(idx) to translate a numeric column number into the corresponding letter.
This solution is quite simplistic, and makes some assumptions about the format of your excel sheets, such as that the data always starts in ColumnA, that an empty cell in ColumnA indicates a totally empty row, and that the heading row always follows the title row. You would also probably want to add some error handling - for example, what if the title row is not found?
Hopefully this can give you a start in the right direction though, and some ideas to try out.
I am currently trying to import an excel sheet into Smartsheet, then take the rows of the imported sheet, and move them to the bottom of an existing sheet. To do this I am using the Sheets.move_row function. Below is a snippet of that code.
response = smart.Sheets.move_rows(
result.data.id,smart.models.CopyOrMoveRowDirective({
'row_ids': [**Help**],
'to': smart.models.CopyOrMoveRowDestination({'sheet_id': 1174866712913796})}))
To get information on the imported sheet I use the get_sheet command. My plan would be to then iterate through the sheet.row property and find where "id" is listed and then pull the number next to id into a comma delimited list.
Below is a snippet of me attempting to iterate through the row property, but I am unsure of how to pull out the row ids, and then put them into a comma delimited list.
sheet_info = smart.Sheets.get_sheet(result.data.id,_dir)
print(sheet_info)
for id in sheet_info.rows:
x = id
print (x) #this just prints the cells category
Any help would be appreciated, thanks. For further clarification on what I am trying to do please reference my previously posted question.
The following code snippet does what you've described.
sheetId = 3932034054809476
# get the sheet
sheet = smart.Sheets.get_sheet(sheetId)
# iterate through the rows array and build comma-delimited list of row ids
row_ids = ''
for row in sheet.rows:
row_ids += str(row.id) + ', '
# remove the final (excess) comma and space from the end of the row_ids string
row_ids = row_ids[:len(row_ids)-2]
print(row_ids)
UPDATE:
As #Isaaclele mentions in the comments below, the Copy or Move Row(s) operation requires that the rowIds parameter be specified as a number[]. The string value of the row_ids property in the code snippet above can be converted to this format as shown here:
row_ids = list(map(int, row_ids.split(',')))
Also note (as mentioned in the comments below) that the Copy or Move Row(s) operation requires the column names in the source sheet and the destination sheet to match exactly.
I'm trying to edit some information in an excel table using python. I have a list of registrations, pickup dates and delivery dates. I want to check if the registration exists in some of the cells of the excel document and then modify its corresponding pickup and delivery dates. I'm fairly new to python as a whole so this task, although at first seeming simple has proved to be quite challenging. How can I locate the specific table ID by checking if it holds a certain value?
Table for reference:
Alright so I solved the problem myself, posting this answer in case somebody else needs to do something similar.
I used the openpyxl library.
//Create a workbook reference an load it using the openpyxl load_workbook()
method by passing in the path to the excel workbook
workbook = openpyxl.load_workbook(workbook_path)
//Craete instance of a worksheet and pass the name of the worksheet you want to edit
current_worksheet = workbook['Sheet1']
//Get the number of rows so that we know how many registrations we need to edit
row_count = current_worksheet.max_row
//Loop through the registrations on the excel sheet, starting from 2 since 1 is the cell
containing "Car Registration"
for j in range(2, row_count):
//Use the worksheet instance and call the cell() method to point to a specific cell and
fetch the registration from that cell
registration_cell = current_worksheet.cell(row = j, column = 1)
current_registration_plate = registration_cell.value
//Compare the fetched registration with the registration we are trying to find. If so
create variables which point to two new cells using the same row where the id was found
(j), add the needed values into them using again .value. and break the loop
if (current_registration_plate == registration_plate):
pickup_cell = current_worksheet.cell(row = j, column = 2)
pickup_cell.value = pickup_value
dropoff_cell = current_worksheet.cell(row = j, column = 3)
dropoff_cell.value = dropoff_value
break
//Save changes to workbook after finishing the loop
workbook.save(workbook_path)
I want my program to read 2 columns (the first and the second one) and add them to an array. They are dependent on eachother - so they need to be written alongside eachother, as in the first row (both columns) next to eachother, and then the second row and so on.
I have managed to write the first column (containing the names) to the array, however have not managed to write the second column to the array.
rownum=1
array=[]
for row in reader:
if row[1] != '' and row[1] != 'Score':
array.append(row[1])
rownum=rownum+1
if rownum==11:
break
I attempted to append more than one row however it returns the error message 'only accepts one argument'.
Any ideas how I can do this so i can reference the score for each name from the csv file
Try using a dictionary.
d = {} #curly braces denote an empty dictionary
for row in reader:
d[row[0]] = row[1]
d, in this case, would be a dictionary with the first column of your csv file as the keys and the second column as the corresponding values.
You can access it very similar to how you access a list. Say you had Brian,80 as one of the entries in your csv file, d["Brian"] would return 80.
EDIT
OP has requested (in the comments) for a more complete version of the code. Assuming OP's code already works, I'll modify that code so it works with a dictionary:
rownum=1
d={} #denotes an empty dictionary
for row in reader:
if row[1] != '' and row[1] != 'Score':
d[row[0]]=row[1] #first column is the key/index, second column is the value
rownum=rownum+1
if rownum==11:
break
This problem is specific wrt using xlrd package in python
I got row of excel which is in form of list but each item is integer value;
type:value
this is not string. The row is save by;
import xlrd
book = xlrd.open_workbook('myfile.xls')
sh = book.sheet_by_index(0)
for rx in range(sh2.nrows):
row = sh.row(rx)
so row saved has value;
row=[text:u'R', text:u'xyz', text:u'Y', text:u'abc', text:u'lmn', empty:'']
This is a list of int. I want the values extracted -
R
xyz
Y
abc
lmn
''
There has to be some method to convert it, but not sure which and how.
Now, I know I can get value just by;
cell_value = sh.cell_value(rowx=rx, colx=1)
but my program requires to collect rows first and then extract values from save row.
Thanks.
The row is a sequence of Cell instances, which have the attribute value.
for cell in row:
cell_value = cell.value
# etc
I am not sure why you want to do it this way - the reference to collecting rows first seems odd to me, given that you can get the rows directly from the worksheet.