Use Python to transfer the contents of the list to Excel

Asked 2 years ago, Updated 2 years ago, 41 views

filename = "File name.xlsx"
book = openpyxl.load_workbook(filename)

# sheet extraction
sheet = book.worksheets[0]

# Enter data
data = []
data_value = data
for row in sheet.rows:
    data.append([
        row[0].value,
        row[1].value,
        row[2].value,
        row[3].value,
        row[4].value,
        row[5].value,
        row[6].value, 
        row[7].value,
        row[8].value,
 ])

First of all, using the above code, we succeeded in extracting the contents of the Excel file as a list.

The resulting value is

["Bunpyeong-dong, Seowon-gu", "All", "All", "All", 99, "24,069", 0.41, 0.5],

["Seowon-gu Sajik 1-dong", "All", "All", "All", 41, "7,139", 0.57, 0.49],

["Seowon-gu Sajik-dong 2", "All", "All", "All", 65, "11,476", 0.57, 0.56],

["Sachang-dong, Seowon-gu", "All", "All", "All", 53, "14,171", 0.37, 0.43]

That's a little bit of it, dozens of it's

The final result I want is

It is to insert the above contents into the cell sequentially from the left in the excel file with blanks.

| Seowon || Full || Full || 99 || 24,069 || 0.41 || 0.5 |

for y in range(2, 9):
    for x in range(3, 46):
        cell = ws.cell(row=x, column=y)
        ws.cell(row=x, column=y)

When I wrote and executed the code above and inserted the data by randomly specifying the value, the value was put in the place I wanted.

How can I insert the contents of the list individually into each cell?

python pandas

2022-09-28 01:00

1 Answers

I think we can proceed like this.

for n, datalist in enumerate(data, 1):
    for n2, i in enumerate(datalist, 1):
        cell = ws.cell(row = n, column = n2)


2022-09-28 01:00

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.