Python Forum

Full Version: Loop through folder of Excel Files and extract single column
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

I have a folder contain multiple folders. Each of these subfolders contains an excel file. Is it possible to loop through each subfolder and create a single CSV from the data from a particular column from each xls file. If possible can I ensure that only excel filenames starting with a certain text are read?

Many thanks for any advice.
yes
Oh, you'd like to know more ...
you can use the package xlwings for excel access: https://pypi.org/project/xlwings/
homepage: https://www.xlwings.org/
for accessing directories, use pathlib:
https://docs.python.org/3/library/pathlib.html

Example (will list contents of home path and a few examples of what you can do with pathlib):
from pathlib import Path
import os

# Assure that starting directory is known(this module directory)
os.chdir(os.path.abspath(os.path.dirname(__file__)))

home = Path('.')

home_files = [filename for filename in home.iterdir() if filename.is_file()]
for filename in home_files:
    print("\n------------------------------")
    print(f"filename: {filename.name}")
    print(f"filename full path: {filename.resolve()}")
    print(f"filename suffix: {filename.suffix}")
    print(f"filename stem: {filename.stem}")
    print(f"filename parts: {filename.parts}")
Thanks a great help