#炫酷# Merge multiple Excel tables in batch

Preface: I encounter a large number of Excel table merging problems to be dealt with in daily office. There are many Excel tables under one folder, and the header of each table is the same, and the format is the same. If you only rely on manual merging, The combination of 3 to 4 photos is relatively OK, but if you have more than 10 photos, you may have a shake of your hand. The combination is wrong and you have to start over again. It's really anti-human. If you have this time, it’s not good to do something else, even if you sleep. , Isn't it fragrant...
Okay, stop talking nonsense, get dry goods

1.1 Demand interpretation

  • Looking at the picture, it is necessary to merge the data of the first sheet page of all Excel files in a certain folder into a sheet page of an Excel file. To put it bluntly, a sheet page must have all the data.
    Combine 12 tables of data
  • The final effect:
    Unified merge into one sheet page
  • Now that the requirements are clear and the results are clear, the next step is to sort out the ideas and see what methods are used.

1.2 Ideas combing (algorithm design)

This process is divided into two parts, the specific implementation steps
need to use the extension library

1.2.1 Implementation steps

  • 1. Get all the files to be merged and transfer them into the memory

  • 2. Create a temporary variable to temporarily store the data in the file, which is also processed in the memory

  • 3. Read the file in a loop and store it in a temporary variable

  • 4. After reading the loop, close the file

  • 5. Create a new file to store data in temporary variables

  • 6. The header of the setting file: model brand sales (units) market share (%) market share year-on-year ±(%) average price (yuan/unit) average price year-on-year ±(%) time to market product specifications product technology

  • 7. Set the content of the file, that is, assign all the data in the temporary variable to the file

  • 8. Save the file (give him a storage path)

1.2.2 Extension library use

  • Need to use the extension library
  • 1. Get all the files under the folder, you need the operating system related library, namely os
  • 2. Read the content of the file, the operation object here is Excel, and the extension library is xlwings
  • 3. Loop body control, use for loop

1.3 Code implementation

# 导入相关扩展库
import os
import xlwings as xw
# 获取所有要合并的文件
files=os.listdir('./20top50量机型')
# 隐式打开文件处理
app=xw.App(visible=False,add_book=True)
data=[]
for file in files:
    f='./20top50量机型/'+file
    wb=xw.Book(f)
    print('正在打开文件....')
    sht=wb.sheets[0]
    data+=sht.range('A2').expand().value
# 创建一个文件
wb_new=xw.Book()
sht_new=wb_new.sheets[0]
# 获取表头
wb_01=xw.Book('./20top50量机型/线上整体.TOP50机型1量.xlsx')
sht_01=wb_01.sheets[0]
sht_header=sht_01.range('A1:K1').value
# 将上述表头作为新文件的表头
sht_new.range('A1').value=sht_header
# 将临时变量中的数据全部赋到新文件中
sht_new.range('A2').value=data
print('合并完成')
# 存储新文件,并重命名
wb_new.save('2020(1-12)销量统计.xlsx')
# 退出app
app.quit()
---------------
正在打开文件....
正在打开文件....
正在打开文件....
正在打开文件....
正在打开文件....
正在打开文件....
正在打开文件....
正在打开文件....
正在打开文件....
正在打开文件....
正在打开文件....
正在打开文件....
合并完成
    

1.4 Result check

Final Results

1.5 Experience

  • The process of designing an algorithm requires careful experience. The implementation of code is similar to the process of human operations/actions, that is, it is simulating human behavior. The real value is to code human repeated behaviors.
  • In daily life or at work, we need to observe carefully, repeated behaviors, repeated operations, repeated processes...
  • Encourage everyone

Guess you like

Origin blog.csdn.net/weixin_42961082/article/details/115041386