Python office combat! Split Excel into separate files by name, and WeChat will automatically send it to the corresponding contact

CoderWanFeng

Hello everyone, this is the Python programmer Wan Feng, today I would like to share with you a manuscript from a reader: Python + Excel automation office, practical application at work .

Welcome everyone to summarize the experience of using pip install python-office, and contact me to contribute~

I. Introduction

Recently encountered a requirement:

  • The employee information summary table excel is divided into separate excels according to the employee's name. The new excel is named after the employee's name. The first row is the header and the second row is the employee information.
  • Then send the excel to each employee individually via WeChat .

This requirement is suitable for scenarios such as sending monthly salary tables and school student information statistics.

I found it on station B. @程序员晚枫The python-office library made by Dashen can perfectly realize this function. I would like to thank you here and serve tea to the boss. I encountered some difficulties during the hands-on operation, and I wrote down my solutions for your reference.

2. Prepare the environment

1. Please apply python 3.8.9 64-bit version

Other versions will encounter various problems when installing the python-office library, which is time-consuming and labor-intensive to solve. It is recommended to use the python 3.8.9 64-bit version directly. You only need to build a new environment on pycharm, and you can easily install and use it.

2. Install python-office

pip install python-office

3. The openpyxl library will also be used to process excel.

Three, thinking analysis

1. Read excel

Use openpyxlthe library to load_workbook()read the corresponding table, and use the form and cell in the form of a list, such as ['Sheet1'].['B1']

2. Delete unused rows in excel

The command to delete the entire line of the openpyxl library is ws.delete_rows()to enter the line number in parentheses. When deleting a line, pay attention to delete it from the back to the front, otherwise the line number will be wrong:

For example, after deleting the second line, you want to delete the third line. At this time, the third line has become the second line, which will cause trouble for the deletion. If you delete it from the back to the front, this problem will not occur.

The largest line will be used here, the command is ws.max_row, and then use for to traverse, you need to count backwards, for i in range(ws.max_row, 1, -1), so you can traverse from the last line until the second line, and ws.delete_rows(i)delete the unwanted ones.

3, save as a separate excel.

wb.save(), write the new file name in parentheses

4. Use the python-office library to send WeChat messages.

We only need to use one line of commands. The developers of the library help us to encapsulate the complexity behind the simplicity. python-officeWe don’t forget to dig the well and give our knees to the great god Wanfeng.

office.wechat.send_file(who=, file=), write the WeChat nickname and file address in parentheses respectively.

Fourth, code display

on the code~

import openpyxl, office #导入两个库,第一个处理excel,第二个用到微信发消息功能,第二个库还有很多强大便捷的功能。
 
wb = openpyxl.load_workbook('C:/CoderWanFeng/./././???.xlsx') #括号中写汇总文件地址
ws = wb['Sheet1']              #获取excel表单
Names = ws['B']                #获取表单中第二列,我的表单第二列是微信昵称,可以根据实际进行调整
max_row = ws.max_row           #获取excel的最大行数
 
for Name in Names:             #第5行代码获得的昵称需要遍历
    Name = Name.value          #遍历出来的是元祖,需要用value进行取值
    if Name == '程序员晚枫':    #我的excel B1单元格写的是程序员晚枫,大家可以根据实际调整
        continue
    else:
        wb = openpyxl.load_workbook('C:/Users/./././???.xlsx')
        ws = wb['Sheet1']
        for j in range(max_row, 1, -1):   #倒着遍历,方便删除时不错序
            if ws[f'B{j}'].value != Name:
                ws.delete_rows(j)         #删除行
        file_path = f'C:/Users/./././{Name}.xlsx'  #重命名
        wb.save(file_path)  #保存excel
        office.wechat.send_file(who=Name, file=file_path)  #通过微信发送文件,分别在括号里写上微信昵称和文件地址。

{{o.name}}
{{m.name}}

Guess you like

Origin my.oschina.net/u/3888978/blog/5581290