Python Forum
Iterate through all files in a folder and make simple calculation
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Iterate through all files in a folder and make simple calculation
#1
Hi,

I want to iterate through a number of .xlsx files in a folder and make a simple calculation for each of the rows in each file. I want to subtract a given number ('compensation') from the first cell (index 0). I then want the calculated value ('corrected_pressure') returned in the adjacent cell (index 1) to the right. I want this to occur for the first cell in every row where there is data. I then want the process iterated for all the .xlsx files in a folder.

I have begun below but I can't seem to get line 5 working;

compensation = 1000

import openpyxl as xl
import Files
for i in Files:
    if i[-4:] =='xlsx':
        wb = xl.load_workbook(i)
        sheet = wb['Content']
        for row in range(2, sheet.max_row+1):
            cell = sheet.cell(row, 2)
            corrected_pressure = cell.value - compensation
            corrected_pressure_cell = sheet.cell(row, 4)
            corrected_pressure_cell.value = corrected_pressure
        wb.save(i)
I get the error;

Error:
Traceback (most recent call last): File "C:/Users/daniel.appleton/PycharmProjects/draft/app.py", line 5, in <module> for i in Files: TypeError: 'module' object is not iterable
Any ideas? Should be a simple fix

DA
Reply
#2
What is Files?
You're importing it and then you iterate over Files, which is not possible, because it's a module.

If you want to get a list of file, use os.listdir or use pathlib.Path.

from pathlib import Path


my_home = Path.home()
my_data = my_home / "Desktop/data"

for file in my_data.glob("*.xlsx"):
    print(file, type(file))
    wb = xl.load_workbook(file)
    # if openpyxl is complaining about file, convert it to a str
    # wb = xl.load_workbook(str(file))
    
If openpyxl doesn't support pathlib objects, then you convert the pathlib object to a str.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
Hi,

Files
is the folder which holds all the files that I want to iterate through.

Is
my_data
(line 5) supposed to equal the location of the .xlsx files that I want to iterate through?

DA
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Compare folder A and subfolder B and display files that are in folder A but not in su Melcu54 3 465 Jan-05-2024, 05:16 PM
Last Post: Pedroski55
  Rename files in a folder named using windows explorer hitoxman 3 692 Aug-02-2023, 04:08 PM
Last Post: deanhystad
  Rename all files in a folder hitoxman 9 1,384 Jun-30-2023, 12:19 AM
Last Post: Pedroski55
  How to loop through all excel files and sheets in folder jadelola 1 4,327 Dec-01-2022, 06:12 PM
Last Post: deanhystad
  python gzip all files from a folder mg24 3 3,807 Oct-28-2022, 03:59 PM
Last Post: mg24
  delete all files and subdirectory from a main folder mg24 7 1,526 Oct-28-2022, 07:55 AM
Last Post: ibreeden
  Merge all json files in folder after filtering deneme2 10 2,251 Sep-18-2022, 10:32 AM
Last Post: deneme2
  Make a python folder .exe Extra 0 999 Jun-10-2022, 01:20 AM
Last Post: Extra
  How would you (as an python expert) make this code more efficient/simple coder_sw99 3 1,749 Feb-21-2022, 10:52 AM
Last Post: Gribouillis
  Compare filename with folder name and copy matching files into a particular folder shantanu97 2 4,390 Dec-18-2021, 09:32 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020