Neculai Fântânaru

Everything Depends On The Leader

How To Import Data From One Excel File To Another Excel File with Python

On May 05, 2021
, in
Python Scripts Examples by Neculai Fantanaru

You can view the full code here: https://pastebin.com/VK2ZV3fJ

import data from excel with python 2

Install Python. Then install the following two libraries using the Command Prompt (cmd) interpreter in Windows10:

import openpyxl #open the bookstore I want to work with

wb_AAA = openpyxl.load_workbook("AAA.xlsx") #open the file in which I import the data
ws_1 = wb_AAA['Date_angajati'] #define the sheet I work with


wb_UI = openpyxl.load_workbook("ttestui.xlsx") #open the file from which I import the data
ws_2 = wb_UI['UI']

sh_obj = wb_UI.active
max_row = sh_obj.max_row
# loop will print all values
# of column1, 2, 4, etc

code2name = {}
for i in range(2, max_row+1):
    cell_obj = sh_obj.cell(row=i, column = 4)
    # print(cell_obj.value)  displays the data in column 4 wb testUI
    code2name[sh_obj.cell(row=i, column=1).value]=(sh_obj.cell(row=i, column=4).value, sh_obj.cell(row=i, column=5).value) #associate the search value (vlookup excel) with the definitions in the testUI
print(code2name)

for i in range (2, ws_1.max_row):
    print(i)
    """
    associate the columns where I import data with the value by which I search for them (vlookup)
    get is the equivalent of "iferror"
    ('','') ...if iferror('value', ''), displays error, in the sense that it searches for the first character out of nothing
    [0][1] refers to line 19, the values after =
    """
    ws_1.cell(row=i, column=7).value = code2name.get(ws_1.cell(row=i, column=6).value, ('',''))[0]
    ws_1.cell(row=i, column=10).value = code2name.get(ws_1.cell(row=i, column=6).value, ('',''))[1]
wb_AAA.save('BBB.xlsx')

The above Python code is the equivalent of the "Vlookup" formula in Excel. That is, it is easier to use this formula to import data without Python.

=vlookup(a;table;x;false)

where: a: the value you are looking for
table: the place where you look for a..usually a table
x: the column containing the value you want to return to you
false: set when looking for an absolute value, not a relative one

See the image below:

I search for a code (adik UI), in table x (which I usually keep in another sheet, or workbook), and it brings me the values from columns G and J
vlookup is a left function .... so if the table has the corresponding column on 5 (adik UI), select the table starting with column 5

This is the table from which to retrieve data with vlookup

import data from excel with python

That's all folks.

If you like my code, then make me a favor: translate your website into Romanian, "ro".

Also, see this VERSION 2 or VERSION 3 or VERSION 4 or VERSION 5 or VERSION 6 or VERSION 7

Alatura-te Comunitatii Neculai Fantanaru
The 63 Greatest Qualities of a Leader
Cele 63 de calităţi ale liderului

Why read this book? Because it is critical to optimizing your performance. Because it reveals the main coordinates after that are build the character and skills of the leaders, highlighting what it is important for them to increase their influence.

Leadership - Magic of Mastery
Atingerea maestrului

The essential characteristic of this book in comparison with others on the market in the same domain is that it describes through examples the ideal competences of a leader. I never claimed that it's easy to become a good leader, but if people will...

The Master Touch
Leadership - Magia măiestriei

For some leaders, "leading" resembles more to a chess game, a game of cleverness and perspicacity; for others it means a game of chance, a game they think they can win every time risking and betting everything on a single card.

Leadership Puzzle
Leadership Puzzle

I wrote this book that conjoins in a simple way personal development with leadership, just like a puzzle, where you have to match all the given pieces in order to recompose the general image.

Performance in Leading
Leadership - Pe înţelesul tuturor

The aim of this book is to offer you information through concrete examples and to show you how to obtain the capacity to make others see things from the same angle as you.

Leadership for Dummies
Leadership - Pe înţelesul tuturor

Without considering it a concord, the book is representing the try of an ordinary man - the author - who through simple words, facts and usual examples instills to the ordinary man courage and optimism in his own quest to be his own master and who knows... maybe even a leader.