0
import openpyxl, pprint
import pandas as pd
import os

After going through everything

my_list = []
for j in range(2, 8):
    my_list.append(sheet1.cell(row = 7, column = j).value)
print(my_list)

I get the following

[1941533, 1002, 0.0005160870301972719, 8523, 1937, 479367.6439999999]

I now want to add each element from my list to excel

for j in range(13,20):
    sheet3.cell(row = 22, column = j)

So I want my_list[0] to = row 22 and column 13 in excel and so on up to J

1 Answer 1

1

I think one of the biggest problems you'll encounter with your code is that my_list has a length of 6, so your second for loop would need to be for j in range(14,20): or for j in range(13,19):.

I've provided a fully reproducible example below. It creates an example workbook called test_1.xlsx, then reads it in and creates a new workbook called test_2.xlsx which has the a second sheet has the list elements added in the row and columns you specified in your question.

import openpyxl, pprint
from openpyxl import load_workbook
import pandas as pd
import numpy as np
import os

# The next four lines create a sample excel file called test_1.xlsx
df = pd.DataFrame(np.random.randn(15, 12), columns=list('ABCDEFGHIJKL'))
writer = pd.ExcelWriter('test_1.xlsx', engine='openpyxl')
df.to_excel(writer, sheet_name='Sheet1', index=False)
writer.close()

wb = load_workbook('test_1.xlsx')
sheet1 = wb['Sheet1']
my_list = []
for j in range(2, 8):
    my_list.append(sheet1.cell(row = 7, column = j).value)
print(my_list)

wb.create_sheet('Sheet3')
sheet3 = wb['Sheet3']
for j in range(13,19):
    sheet3.cell(row = 22, column = j).value = my_list[j-13]
wb.save("test_2.xlsx")
Sign up to request clarification or add additional context in comments.

1 Comment

my_list = [cell.value for cell in ws[22][2:8]]

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.