Python Forum
Loop through folder of Excel Files and extract single column
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Loop through folder of Excel Files and extract single column
#1
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.
Reply
#2
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}")
Reply
#3
Thanks a great help
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Is it possible to extract 1 or 2 bits of data from MS project files? cubangt 8 945 Feb-16-2024, 12:02 AM
Last Post: deanhystad
  File loop curiously skipping files - FIXED mbk34 10 685 Feb-10-2024, 07:08 AM
Last Post: buran
  Copy Paste excel files based on the first letters of the file name Viento 2 348 Feb-07-2024, 12:24 PM
Last Post: Viento
  Compare folder A and subfolder B and display files that are in folder A but not in su Melcu54 3 466 Jan-05-2024, 05:16 PM
Last Post: Pedroski55
  Rename files in a folder named using windows explorer hitoxman 3 693 Aug-02-2023, 04:08 PM
Last Post: deanhystad
  Rename all files in a folder hitoxman 9 1,387 Jun-30-2023, 12:19 AM
Last Post: Pedroski55
Question Need help for a python script to extract information from a list of files lephunghien 6 1,033 Jun-12-2023, 05:40 PM
Last Post: snippsat
  How to loop through all excel files and sheets in folder jadelola 1 4,333 Dec-01-2022, 06:12 PM
Last Post: deanhystad
  Creating csv files from Excel file azizrasul 40 5,329 Nov-03-2022, 08:33 PM
Last Post: azizrasul
  python gzip all files from a folder mg24 3 3,814 Oct-28-2022, 03:59 PM
Last Post: mg24

Forum Jump:

User Panel Messages

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