SoFunction
Updated on 2025-05-14

Share the 10 most commonly used Python libraries for office automation

Today I want to talk to you about something practical and useful,—The 10 most commonly used Python libraries for office automation

To be honest, I have personally tested these libraries, not the kind that can be summarized by just a few lines online.
They are all real artifacts that make you fishing at work, easy to get off work, and the boss praises you for being "unreasonably efficient"!

OK, let’s not talk much, just open the market!

1. openpyxl – the king of operation of Excel

I remember one time I worked overtime to process a huge Excel file, and the data was so much that my mouse circled, and I began to doubt my life.
As a result, openpyxl was done in a few lines of code, and I was almost moved to tears on the spot.

Basic usage

from openpyxl import load_workbook

wb = load_workbook('')
sheet = 
print(sheet['A1'].value)
sheet['A1'] = 'Hello, world!'
('example_modified.xlsx')

Small details reminder

Openpyxl only supports by default.xlsxFormat, old version.xlsTo usexlrdUse it in conjunction, otherwise an error will be reported!

2. pandas — Process table data, God!

Speaking of office, table data is definitely a severely affected area. I am almost confused by the function when dealing with Excel every day.

Once, the leader gave me a bunch of sales data and said in an indifferent tone: "Please calculate the total amount and average value of this, and classify it, give it to me early." I was still holding milk tea in my hand at that time, and almost couldn't suck it up...

With my brain twitching, I opened pandas, and the results were produced in 10 minutes, the chart was added in 20 minutes, and the email was packaged in 30 minutes. When the leader saw it, "Well, it's done well, it's very meticulous." I pretended to be calm and had already been secretly happy:If there is no pandas, I will work overtime until 10 o'clock tonight

Simple example

import pandas as pd

df = pd.read_excel('')
print(())
total = df['Sales'].sum()
print(f"Total sales:{total}")

Notice

Before installing pandas, remember to install itopenpyxl, otherwise it will be directread_excelThere will be a hint of dependency when it is not available!

3. python-docx — a brick-moving expert in Word documents

Once I wrote a work summary, and the boss asked for a unified format and font, and manually modify it? Impossible, it will never be possible in this life.
python-docx easily helps me automatically generate Word reports, drinking coke while running scripts, making me happy.

Basic usage

from docx import Document

doc = Document()
doc.add_heading('Zhou Summary', 0)
doc.add_paragraph('The following tasks were completed this week:...')
('weekly_report.docx')

Small details

The order of paragraphs and styles in Word is very important when operating docx, and it may be messed up by accident. It is recommended to try a few more times and step on the pit!

4. PyPDF2 — Merge and split PDFs and easily handle them

If it weren't for PyPDF2, I might have to use Adobe to split and merge PDFs manually.
Using Python, batch processing in a few seconds, it is very comfortable.

Simple example

import PyPDF2

merger = ()
('')
('')
('')
()

Small reminder

If you encounter an encrypted PDF, PyPDF2 can decrypt it, but you have to know the password.

5. smtplib + email — Automatic mass email, don't be too cool

For a while, I helped the HR girl send a group offer email, each with a resume and a notification.
At first, I sent 5 messages manually... Later I thought about it, it was not right! I immediately used smtplib to create a small script to send emails automatically, and took off instantly🛫.

Code Example

import smtplib
from  import MIMEText

msg = MIMEText('Hello, congratulations on passing the interview!  ')
msg['Subject'] = 'Admission Notice'
msg['From'] = 'me@'
msg['To'] = 'you@'

server = ('', 587)
()
('me@', 'password')
server.send_message(msg)
()

Small details

Some companies have very strict mailbox servers. Remember to ask the IT department’s SMTP settings in advance!

6. schedule — Timed execution assistant

Sometimes I want to run scripts regularly, such as posting a daily newspaper at 8 am every morning.
Don't have to stare at it yourself, the schedule is like a small alarm clock, ting it and it runs automatically.

Simple example

import schedule
import time

def job():
    print("Send daily newspapers at 8 o'clock every day")

().("08:00").do(job)

while True:
    schedule.run_pending()
    (1)

7. pyautogui — Simulate mouse and keyboard operation, so slutty

I remember that once the system login page did not have an API, so I could only click it manually.
Later, I was furious and wrote a pyautogui script, which automatically opened the browser, entered the account, and typed in the car. I didn’t have to do it all the way, which was so fulfilling!

Example

import pyautogui

(100, 200)
()
('hello world')
('enter')

Detailed reminder

The operation speed is too fast and may cause the interface to not react. You can add some = 1.0, prevent losing control ~

8. xlwings — Excel artifact, take you flying!

To put it bluntly, openpyxl is OK to handle static tables. If you want to directly operate "open" Excel files? That's still goingxlwings
I used it to make a gadget to automatically fill in the quotation form, and knocked on Enter, and filled in a lot of forms automatically. My colleagues next to me were stunned.

Example

import xlwings as xw

wb = ('')
sheet = ['Sheet1']
('A1').value = 'Hello, Excel!'
()
()

9. tqdm — Make your script handsome (with progress bar)

Some scripts run very slowly, and I feel very nervous when I see the command line blank.
OnePlus, the progress bar rolled, and I felt much more at ease in an instant.

usage

from tqdm import tqdm
import time

for i in tqdm(range(100)):
    (0.1)

10. os + shutil — Must-have for batch processing of files

Move files, renames, copying, etc., Python comes withosandshutilIt's already very useful.
Especially in conjunction with glob batch screening, one-stop service.

example

import os
import shutil

files = ('data')
for file in files:
    if ('.txt'):
        (('data', file), 'backup')

The above are the detailed content shared by the 10 most commonly used Python libraries for office automation. For more information about the Python office automation library, please pay attention to my other related articles!