gpt4 book ai didi

Get value of a cell coordinated by 2 (or more) cells from different excel files and put it all in a general file(从不同的Excel文件中获取由2个(或更多)单元格协调的单元格的值,并将其全部放入一个通用文件中)

转载 作者:bug小助手 更新时间:2023-10-25 22:50:59 26 4
gpt4 key购买 nike



I have this board here, it's from an Excel file of a merchant:

我这里有一块牌子,它来自一个商人的Excel文件:


Excel file


I want to extract the value of 3 cells, as circled below:

我想提取3个单元格的值,如下所示:


Values needed


and put it on a general Excel file, like this:

并将其放在一个通用的Excel文件中,如下所示:


Required file


I usually copy and paste manually, but the merchant number is increasing and it's like 300-400 by now, so I need to automate the task. Any solution using VBA, query or Python is welcome.

我通常手动复制和粘贴,但商家数量正在增加,现在已经有300-400家了,所以我需要自动执行这项任务。欢迎任何使用VBA、Query或Python的解决方案。


更多回答

You circled 4,407,482 as a value that you want in the output, but I don't see it in the desired output.

您圈出了4,407,482作为您希望在输出中使用的值,但我没有在期望的输出中看到它。

You should paste the values here, not link to images.

您应该将值粘贴到此处,而不是链接到图像。

See how to ask and mention what you tried and why it failed before asking a question.

了解如何在提问前询问和提及你尝试过的内容以及失败的原因。

What's resource of the 2nd and 3rd columns in the output?

输出中第二列和第三列的资源是什么?

优秀答案推荐

Some things need to guessed at but this should help.

It appears you want to copy the values from cells 'E4' to 'E6' to the other sheet.
I don't know if

有些事情需要猜测,但这应该会有所帮助。您似乎想要将单元格‘E4’到‘E6’中的值复制到另一个工作表。我不知道如果



  1. the table is fixed in size and the only data to be copied will always be in cells 'E4' to 'E6' or if you'd also want to copy data from cells 'E8' to 'E10'

  2. if all data will always exist as shown in the current cells or weather the rows will change be missed or added to. Same with columns

  3. if the STT numbers are supposed to match the 'Payment Method' description or will be different in any other ways. I notice that 'E-wallet' has the STT number 2 in the first group and 3 in the second group. This may be an error but for the example it does not affect anything.




Using the Openpyxl module we can check column 'A' for a cell value as reference and then use offsets.




For this example I am using the modified table shown below with data filled in cell 'E4' to 'E6' and 'E8' to 'E10' so the copying of data can be seen.

I'll use the 'STT' column A as the reference and the number '1' in that column as the location for the first row with data to copy, i.e. matches the 'Payment Method' 'International Card'.

So in the example the code checks column 'A' for the value 1 and then copies the next 3 values from 'E' column to the other table. Then looks for the next value 1 and copies those three values to the next 3 rows and so on.

You are not clear where/how the data should appear in the other table so I have copied all to column 'F' in line with the headings.

enter image description here

使用Openpyxl模块,我们可以检查列‘A’中的单元格值作为引用,然后使用偏移量。在本例中,我使用了下面显示的修改后的表,其中的数据填充在单元格‘E4’到‘E6’和‘E8’到‘E10’中,因此可以看到数据的复制。我将使用‘STT’列A作为参考,并将该列中的数字‘1’用作要复制数据的第一行的位置,即匹配‘支付方式’‘国际卡’。因此,在本例中,代码检查列‘A’中的值1,然后将接下来的3个值从‘E’列复制到另一个表。然后查找下一个值1,并将这三个值复制到下一个3行,依此类推。您不清楚数据应该出现在另一张表中的何处/如何显示,因此我已将所有内容复制到与标题一致的F栏中。


Code sample;

For this example I'm assuming that the tables exist in the same workbook. The originating table on 'Sheet1' and the destination table on 'Sheet2'.

代码示例;对于本例,我假设这些表存在于同一工作簿中。‘Sheet1’上的始发表和‘Sheet2’上的目的表。


from openpyxl import load_workbook

### Open the Excel workbook
file_path = 'foo.xlsx'
wb = load_workbook(file_path)
### Assign the two Sheets
ws1 = wb['Sheet1']
ws2 = wb['Sheet2']

### ws2_row is the first row to start entering data on table 2 (row 1 is header row)
ws2_row = 2 # Start from row 2

### Copy the data
for cell in ws1['A']: # Loop through column A
if cell.value == 1: # Check if the cell value is integer 1
### If cell value is 1 then the row is 'International card' row
for x in range(0, 3): # Loop for the next 3 values
head = cell.offset(row=x, column=1).value # This is for printing only
print(f'Payment Method: {head}', end='') # Prints the 'Payment method' description
### Get the value in the cell on the same row in column E (offset 4)
val = cell.offset(row=x, column=4).value
### Copy the value of this cell to Sheet 2 table column E
ws2[f'E{ws2_row}'].value = val
print(f' : {val}')
ws2_row += 1 # Increment to the next row
print("---------------------------------------")

wb.save('foo_out.xlsx')

The table on sheet 2 with then have the values as shown;

Table 2 on Sheet2

表2中的值如图所示;


This should give you some idea (using Openpyxl).

You may need to change how the location of the data is determined, It could be from the description in col B or if the table layout is always the same then just use the cell values directly.

If the data needs to be copied to different cells in table 2 you'll need to change the destination(s) and otherwise adjust the code to suit your actual requirement.

这应该会让您对(使用Openpyxl)有所了解。您可能需要更改数据位置的确定方式,这可能来自列B中的描述,或者如果表格布局始终相同,则只需直接使用单元格值。如果需要将数据复制到表2中的不同单元格,您将需要更改目标(S),否则将调整代码以适应您的实际需求。


更多回答

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com