Python: Copy Data From Multiple Files to Master File | Read/Write Closed Excel Files Using Openpyxl

preview_player
ะŸะพะบะฐะทะฐั‚ัŒ ะพะฟะธัะฐะฝะธะต

๐——๐—˜๐—ฆ๐—–๐—ฅ๐—œ๐—ฃ๐—ง๐—œ๐—ข๐—ก
โ–€โ–€โ–€โ–€โ–€โ–€โ–€โ–€โ–€โ–€โ–€โ–€โ–€โ–€โ–€โ–€โ–€โ–€โ–€โ–€โ–€โ–€โ–€โ–€โ–€โ–€
This tutorial will show you how to read & write closed Excel Files in Python using openpyxl. In particular, we will copy data from multiple Excel files in one Master workbook. After executing the Python script, openpyxl will create a new workbook that combines all the single Excel files.

โœ… ๐—ง๐—ถ๐—บ๐—ฒ๐˜€๐˜๐—ฎ๐—บ๐—ฝ๐˜€:
00:00 | Problem Statement and Final Outcome
01:26 | Solution Approach
02:30 | Step 1
02:52 | Step 2
04:37 | Step 3
06:43 | Wrap Up

๐Ÿ“ ๐—ฅ๐—ฒ๐˜€๐—ผ๐˜‚๐—ฟ๐—ฐ๐—ฒ๐˜€:
Download the Python File & Excel Sample Files here [Google Drive]:

โ—ผ๏ธโ—ผ๏ธโ—ผ๏ธโ—ผ๏ธโ—ผ๏ธโ—ผ๏ธโ—ผ๏ธโ—ผ๏ธโ—ผ๏ธโ—ผ๏ธโ—ผ๏ธโ—ผ๏ธโ—ผ๏ธโ—ผ๏ธโ—ผ๏ธโ—ผ๏ธโ—ผ๏ธ
๐Ÿ‘ฉโ€๐Ÿ’ป ๐—ฃ๐˜†๐˜๐—ต๐—ผ๐—ป ๐—–๐—ผ๐—ฑ๐—ฒ:
from pathlib import Path # Standard Python Module
from openpyxl import load_workbook, Workbook # pip install openpyxl

# -- 1.STEP
# Get all excel file paths from given directory
SOURCE_DIR = "Daily_Reports" # e.g. r"C:\Users\Username\Desktop\Sample Files"
excel_files = list(Path(SOURCE_DIR).glob("*.xlsx"))

# -- 2.STEP:
# Iterate over all Excel files from step 1,
# access the worksheet and store the values in a dictionary
# values_excel_files = {['2021-01-01'] : [1,2,3, ..],
# ['2021-01-02'] : [1,2,3, ..]}
values_excel_files = {}
for excel_file in excel_files:
wb = load_workbook(filename=excel_file, read_only=True)
rng = wb["Sheet1"]["B2":"B19"]
rng_values = []
for cells in rng:
for cell in cells:
values_excel_files[report_date] = rng_values

# -- 3.STEP:
# a) Iterate over all worksheets in the master workbook
# b) For each worksheet, iterate over defined Excel range (dates)
# c) If date matches with the key of dictionary (values_excel_files) then insert values & save workbook
clm = "B"
first_row = 3
last_row = len(ws[clm])
rng = ws[f"{clm}{first_row}:{clm}{last_row}"]
for cells in rng:
for cell in cells:
# Iterate over values (list inside the dictionary) and write values to column

๐—ง๐—ข๐—ข๐—Ÿ๐—ฆ ๐—”๐—ก๐—— ๐—ฅ๐—˜๐—ฆ๐—ข๐—จ๐—ฅ๐—–๐—˜๐—ฆ
โ–€โ–€โ–€โ–€โ–€โ–€โ–€โ–€โ–€โ–€โ–€โ–€โ–€โ–€โ–€โ–€โ–€โ–€โ–€โ–€โ–€โ–€โ–€โ–€โ–€โ–€

๐—–๐—ข๐—ก๐—ก๐—˜๐—–๐—ง ๐—ช๐—œ๐—ง๐—› ๐— ๐—˜
โ–€โ–€โ–€โ–€โ–€โ–€โ–€โ–€โ–€โ–€โ–€โ–€โ–€โ–€โ–€โ–€โ–€โ–€โ–€โ–€โ–€โ–€โ–€โ–€โ–€โ–€

โ˜• ๐—•๐˜‚๐˜† ๐—บ๐—ฒ ๐—ฎ ๐—ฐ๐—ผ๐—ณ๐—ณ๐—ฒ๐—ฒ?
ะ ะตะบะพะผะตะฝะดะฐั†ะธะธ ะฟะพ ั‚ะตะผะต
ะšะพะผะผะตะฝั‚ะฐั€ะธะธ
ะะฒั‚ะพั€

Thank you. Good job! :) Thanks for the content.

With love, from South Africa.

compton
ะะฒั‚ะพั€

Nice video flow design. Useful stuffโ€ฆ!

asankacool
ะะฒั‚ะพั€

u are amazing kudos to your effortโค๐Ÿ‘ thanks

sayantandatta
ะะฒั‚ะพั€

Hi Sven, danke fรผrs Video. Ist es mรถglich die Rows und Columns im Mastertemplate zu drehen, also das Datum entlang der Colums und die Produkte in A bis nach unten. Aber versucht den Code umzuschreiben, ging aber nicht

mikekaspari
ะะฒั‚ะพั€

Great tutorial again! ๐Ÿ‘Œ

Really helpful but in my case, i would like to create a new sheet for each excel files but sadly, it throws error.
Example,
wb =
wb.create_sheet("test")

Then, i get a TypeError : expected str... not list

ahmadmojeed
ะะฒั‚ะพั€

Thanks for the tutorial.
One problem, the data is not stored as values but are formulas, and the solution in the video is not fetching values !
How to modify the code to get values?

aquabestindian
ะะฒั‚ะพั€

Hai sir, ur video super. I learn so much ur channel. I have one doubt I'm using excel data python. Excel sheet cells I have created formula. Excel read data python program only copy formula not value. Please help me I need excel rows read values only. Please advise sir...

royalwinautomatedsoftware
ะะฒั‚ะพั€

This may be a silly question, but I'm trying to add data from a excel file into a template without the date comparison. The idea is to grab everything from A2:K99. I can't seem to get this going :)

How can I modify this script to do this?

marcelduplessis
ะะฒั‚ะพั€

This is really a great tutorial Sir.

I am new in this sector. I am trying to merge multiple excel files into one master excel file. The code runs well using pandas, but it removes all the formatting from master excel file. I saw this video and found that the formatting in the master excel file remains as it is. Is it because of the openpyxl package instead of pandas?

sachinmahale
ะะฒั‚ะพั€

Hi Sven, each of the files I need to combine into one workbook has a date in its title. I would like to get this title and copy it into a column I will create in each file so that at the end, I have all the rows with the titles from the files they have been copied from. How can I do it? Many thanks!

gregorydubois
ะะฒั‚ะพั€

How can we make the code work if the range within each workbook changes?

akshayathalye
ะะฒั‚ะพั€

but if i want to do the opposite, dividing the data from the master workbook to multiple excel file how should i proceed ??

rosalyna_
ะะฒั‚ะพั€

What if the worksheets have different names?

Yoitsryn
ะะฒั‚ะพั€

Great tutorial Sir. I subscriber to your channel as there a lot of tutorial that would help me in my work.

Sir Im just started to learn python and I badly needed to learn because my work requires a lot of automation in excel. Appreciate sir if you can help me for one basic example. Need to automate copy and pasting range of cells from one multiple work book to Master excel file. Can you help me with to code copying range of data from column B2:B28 of sheet name "raw" of excel file name "paging" to paste in master excel file sheet name "CSSR" column AL2:AL28 and next day same source file to the next column AM2:MA28 and AN2:AN28 and soon. Thank you sir waiting for your help

noeldoller
ะะฒั‚ะพั€

1. The interface to write python code, I see it seems to be localhost8888. but when i access it it is blocked, is there any way to solve this problem?
2. Extract all info of 1 video or 1 playlist or all videos of 1 youtube channel into excel file, use python can work ?

tanganhminh
ะะฒั‚ะพั€

Hi, I get the following error
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated escape

Any idea how to get round this ?

andrewwithers
ะะฒั‚ะพั€

Great video thanks for sharing, I just found this channel, do you happen to have a video on how to copy from multiple .xlsx source files just like yours but paste in multiple individual destination .xlsm files that are all in individual sub folders eg copy data from 20210101_Report.xlsx and paste in existing file 20210101_Sales.xlsm, copy data from 20210102_Report.xlsx and paste in existing file 20210102_Sales.xlsm where is 20210101_Sales.xslm is in a sub folder 'C:/folderpath/Jan1/' and 20210102_Sales.xlsm is in another sub folder 'C:/folderpath/Jan2/'

ohmandy
join shbcf.ru