Bottom Page

Thread Rating:
  • 2 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Looping .xlsx files in folder/subfolders copy pasting currentregion
#1
How to loop folders and subfolders selecting currentregions associated with a key word?

I want to loop excel (.xlsx) files in a folder and subfolders, searching for a key word ('trust') in column A, of worksheet 'data', and selecting the currentregion with offset 1 row, 0 columns.

I am from a VB background hence the term 'currentregion', so if the word 'trust' was in cell ("A2"), I want the currentregion (contiguous populated area from cell ("A3").

I want to copy paste these data ranges into a master workbook called 'master.xlsx', pasting the data to sheet 'm_data', into the next available empty row in column "A".

I know I can import os for looping, presumably with a file mask, and openpyxl to read and write with .xlsx, I am not sure how to select currentregion in each file and copy to next empty row in the master file.

Any help/pointers would be most helpful.

I am using the latest python3 interpreter and openpyxl module in pycharm/ Cygwin with python on windows 7 x64.

Thanks, 

Q

*edit has failed.

I also wanted to know if I could use shelve and a shelfFile to maintain a list of the processed files.
I want to skip a file if it has been processed before. This would require filepath and filename as files in different subfolders may have the same name.
Quote
#2
Use os.walk() for iteration over directory tree. The rest - show some effort
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Quote
#3
Thanks. It is the text search with selection of currentregion offset (1,0) that i am particularly interested in if you have any suggestions regarding this?
Google with python and currentregion keeps returning VB references, perhaps unsurprisingly. 

Q
Quote
#4
I barely use openpyxl, but since cells of worksheet are addressable by column and row - see this example, iterating over row indices like that will give you cell values 
ws['A{}'.format(row_index)].value
PS Using other language terminology - who the f*ck is Alice, sorry VB Tongue does not help to understand you. Just talk cells, rows and columns - those are clear enough  Wink
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Quote
#5
Thanks
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Loop through folder of Excel Files and extract single column fioranosnake 2 99 Oct-28-2019, 05:19 PM
Last Post: fioranosnake
  Replace string in many files in a folder metro17 8 314 Oct-16-2019, 06:46 PM
Last Post: ndc85430
  How to copy files from subfolders into one folder silfer 3 177 Aug-29-2019, 08:36 AM
Last Post: DeaD_EyE
  Python Script to repeat Photoshop action in folders and subfolders silfer 2 224 Jul-25-2019, 03:12 PM
Last Post: silfer
  how to move files to another folder agatte 1 175 Jul-24-2019, 01:27 PM
Last Post: ichabod801
  How do I copy files faster with python? steckinreinhart619 7 912 Jul-19-2019, 11:47 AM
Last Post: perfringo
  Looping through music files (SOLVED) ebolisa 0 219 Jul-13-2019, 06:16 PM
Last Post: ebolisa
  Encoding problems on multiple files in one folder NikolajKorsgaard 5 542 Jun-11-2019, 03:39 AM
Last Post: micseydel
  Delete Lines that Contain Words - Loop through files in a folder - Write to new files dj99 3 442 May-18-2019, 06:34 AM
Last Post: heiner55
  Read a folder with a multiple files NewBeie 7 506 May-06-2019, 08:04 AM
Last Post: NewBeie

Forum Jump:


Users browsing this thread: 1 Guest(s)