如何将来自一个Excel文件的数据导入另一个Excel文件与Python
ro  fr  en  es  pt  ar  zh  hi  de  ru
ART 2.0 ART 3.0 ART 4.0 ART 5.0 ART 6.0 Pinterest

如何将数据从一个Excel文件导入另一个Excel文件与Python

On May 05, 2021, in Leadership and Attitude, by Neculai Fantanaru

您可以在此处查看完整代码:HTTPS://帕萨特斌.com/VK2Z V3FJ

import data from excel with python 2

安装Python。 然后使用Windows10中的命令提示符(CMD)解释器安装以下两个库:

py- m pip install openpyx

代码:在任何翻译程序中复制并运行下面的代码(我在用Pycripter) .

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')

上述Python代码是Excel中的“Vlookup”公式。 也就是说,更容易使用此公式来导入没有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: SHARE IT.

其他Python代码: BeautifulSoup 要么 Google Translate API Key 要么版本3.要么版本4.要么版本5.


Latest articles accessed by readers:

  1. An Eye To See And A Mind To Understand
  2. Turn Towards Me With An Eye Full Of Your Own Gaze
  3. The Snapshot Of Magic In God's Universe
  4. Rhythm Of My Heart

Donate via Paypal

Alternate Text

RECURRENT DONATION

Donate monthly to support
the NeculaiFantanaru.com project

SINGLE DONATION

Donate the desired amount to support
the NeculaiFantanaru.com project

Donate by Bank Transfer

Account Ron: RO34INGB0000999900448439

Open account at ING Bank

Join The Neculai Fantanaru Community



* Note: If you want to read all my articles in real time, please check the romanian version !

decoration
About | Site Map | Partners | Feedback | Terms & Conditions | Privacy | RSS Feeds
© Neculai Fântânaru - All rights reserved