0

I have an excel sheet with two different columns from where I want to take data one at a time say data value from A2 and B2 then perform certain operation and then again take values from B3 and C3 and so on.. till B50 and C50. I have written the following code for this but I believe it's not correct. Also I am new to python so don't have much knowledge. Any help would be appreciated. Thanks in advance!

    wb = openpyxl.load_workbook('Inputexcel.xlsm')
    sheet = wb['Sheet1'] 
    tup1 = (sheet['A2':'A50'])
    tup2 = (sheet['B2':'B50'])
    for i in range(0,48):      
    text = tup1[i].value
    i+=1

I was able to implement it for a single value and also perform the operation by using the below code. But my main aim is to iterate for all the values of excel where I am facing issue. Code for single value :

    wb = openpyxl.load_workbook('Inputexcel.xlsm')
    sheet = wb['Sheet1']
    text = sheet['A2'].value
    eid = sheet['B2'].value

Also sharing the Operation to be performed if it is of any help :

    doc = docx.Document('mydoc.docx')
    doc.paragraphs[9].runs[6].clear()
    doc.paragraphs[9].runs[6].text
    doc.paragraphs[9].add_run(text).bold = True
    doc.save('mydoc ' + str(eid) + '.docx')

1 Answer 1

1

Instead of adding the Cells in tup1 and tup2 - I would recommend adding the values directly to tup1 and tup2 ...like below:

wb = openpyxl.load_workbook('Book1.xlsx')
sheet = wb['Sheet1'] 
tup1 = []
tup2 = []
for i in range(2,50):
    rowA ='A{0}'.format(i)
    rowB = 'B{0}'.format(i)
    tup1.append(sheet[rowA].value)
    tup2.append(sheet[rowB].value)
print (tup1)
print (tup2)
Sign up to request clarification or add additional context in comments.

1 Comment

The irony of calling a list tup :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.