I have a spreadsheet with data on people, in which first names are in cells in column L and last names are in the cells in column M. I need to combine these into variables that contain both the first and last name for each person.
import openpyxl
wb = openpyxl.load_workbook('/Users/userName/Desktop/sheetName.xlsx')
main = wb['Sheet1']
The following code will do this for a single row, but I need to make it iterate through every row containing data.
cell1 = main['L1'].value
cell2 = main['M1'].value
cell3 = cell1+cell2
print(cell3)
Here is what I'm trying, but I keep getting the error message name 'rows' is not defined
combined = []
for i in main.rows:
combined.append(i.value for i in row)
print(combined)
Thanks in advance for help with this puzzle!
This is what I got working:
import openpyxl
wb = openpyxl.load_workbook('/Users/userName/Desktop/testSheet.xlsx')
main = wb['Sheet1']
for row in range(2, main.max_row +1):
firstName = main["L" + str(row)].value
lastName = main["M" + str(row)].value
fullName = firstName + ' ' + lastName
print(fullName)
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.