0

I want a import some columns from an excel spreadsheet, however, I also want to run some lines of code on each column. So i need it to read a specific column, run some lines of code and then return to pick another column and do the same thing. I'm guessing this will work with an iterative loop but I'm new to pyhton so I can't seem to get the syntax right. For now this is what I have which is very manual:

import openpyxl
import numpy as np
book = openpyxl.load_workbook('new.xlsx')
sheet = book.active

nx = 19
ny = 19

rangedata = []
for c in sheet.iter_cols(4,4,2,362):
    columndata = []
    for  r in c:
        columndata.append(r.value)
cd = np.asarray(columndata)
P = np.flip(cd.reshape(nx,ny), axis=0)

rangedata = []
for d in sheet.iter_cols(4,5,2,362):
    Column = []
    for  s in d:
        Column.append(s.value)
nd = np.asarray(Column)
Q = np.flip(nd.reshape(nx,ny), axis=0)

I've tried using but it is only returning the last column

rangedata = []
for col_cells in sheet.iter_cols(min_col=4, max_col=8):
    for cell in col_cells:
for c in sheet.iter_cols(4,8,2,362):
    columndata = []
    for  r in c:
        columndata.append(r.value)
cd = np.asarray(columndata)
P = np.flip(cd.reshape(nx,ny), axis=0)
  • if your data isn't huge (hundreds of thousands of rows) would it be acceptable to import the sheet as a dictionary (if it is just one table)? Then you can run the calculations all in python and rewrite to the excel sheet, just another way of thinking of it. Also is numpy absolutely necessary? – bherbruck May 16 '20 at 01:09
  • It is huge , very huge infact. – ademola adelakun May 16 '20 at 01:15
  • it will be helpful if u share some data, with a guide on what u want to do. [minimal, reproducible example](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – sammywemmy May 16 '20 at 02:18
  • I want to import data from an excel sheet column by column. Each column contains pressure data at different time steps. Calculations will be made on each column. So what I need is a syntax, that takes each column, run said calculations, store the data and return back to the excel sheet and take the next column until the last column. I don't know if this is possible. – ademola adelakun May 16 '20 at 02:27
  • is it only in excel or also in csv? – bherbruck May 16 '20 at 04:39
  • I think I can transfer to CSV – ademola adelakun May 16 '20 at 09:52

0 Answers0