In daily office work, we often take up a lot of time by tedious and repetitive tasks. As a powerful programming language, Python has rich libraries and tools that can easily achieve office automation and greatly improve work efficiency. Today, I will share with you 10 practical Python automated office cases and source codes.
1. Batch processing of Excel files
When processing data, it is often necessary to do the same thing with multiple Excel files. usepandas
The library can be easily implemented.
import pandas as pd import os # folder pathfolder\_path = 'your\_folder\_path' for filename in (folder\_path):   if ('.xlsx'):   file\_path = (folder\_path, filename)   df = \_excel(file\_path)   \# Here you can perform various operations on df, such as adding a new column  df\['new\_column'] = df\['Original Column'] \* 2  \_excel(file\_path, index=False)
2. Automatically send emails
usesmtplib
andemail
library, automatic email sending, suitable for regular reporting and other scenarios.
import smtplib from import MIMEText from import Header # Sender Emailsender = "your\_email@" # Recipient emailreceivers = \["recipient\_email@"] # Email contentmessage = MIMEText('Email Content', 'plain', 'utf-8') message\['From'] = Header("Sender's Name", 'utf-8') message\['To'] = Header("Recipient's Name", 'utf-8') message\['Subject'] = Header("Email Subject", 'utf-8') try:   smtpObj = ('', 587)   ()   (sender, "password")   (sender, receivers, \_string())   print("Mail sent successfully")except as e:   print("Error: Cannot send email", e)
3. Batch rename files
useos
The library renames files in the specified folder in batches.
import os folder\_path = 'your\_folder\_path' count = 1 for filename in (folder\_path):   if ((folder\_path, filename)):   new\_name = f'new\_name\_{count}{(filename)\[1]}'   ((folder\_path, filename), (folder\_path, new\_name))   count += 1
4. Data cleaning
usepandas
The library cleanses the data, removes duplicate values, processes missing values, etc.
import pandas as pd df = \_csv('your\_data.csv') # Remove duplicate rowsdf = \_duplicates() # To process missing values, fill them with 0 heredf = (0) \_csv('cleaned\_data.csv', index=False)
5. Generate PPT
With the helppython-pptx
The library can automatically generate PPT based on the data.
from pptx import Presentation from import Inches prs = Presentation() title\_slide\_layout = \_layouts\[0] slide = \_slide(title\_slide\_layout) title = subtitle = \[1] = "PPT Title" = "PPT Subtitle" # You can add more content in the future, such as pictures, tables, etc.('')
6. Automated testing
useSelenium
The library performs web page automation testing.
from selenium import webdriver driver = () ('') # Find elements and operateelement = \_element\_by\_id('element\_id') () # Close the browser()
7. Extract PDF text
usePyPDF2
Library extracts text from a PDF file.
import PyPDF2 pdf\_file = open('your\_pdf.pdf', 'rb') pdf\_reader = (pdf\_file) text = "" for page\_num in range(len(pdf\_reader.pages)):   page = pdf\_reader.pages\[page\_num]   text += \_text() print(text) pdf\_file.close()
8. Automatically generate reports
Combinedpandas
andmatplotlib
library, generate data reports and visualize them.
import pandas as pd import as plt df = \_csv('') # Assume that statistics of a column of datadata = df\['column\_name'].value\_counts() (kind='bar') ('Data statistics report') ('category') ('quantity') ('')
9. Automate file backup
useshutil
The library implements automatic backup of files.
import shutil import os source\_folder = 'your\_source\_folder' backup\_folder = 'your\_backup\_folder' if not (backup\_folder):   (backup\_folder) for filename in (source\_folder):   file\_path = (source\_folder, filename)   if (file\_path):   shutil.copy2(file\_path, backup\_folder)
10. Task Scheduling
useAPScheduler
The library implements the timing execution of tasks, such as running data processing scripts regularly.
from import BlockingScheduler import your\_script scheduler = BlockingScheduler() # Execute tasks at 1 a.m. every day\_job(your\_script.run, 'cron', hour=1) ()
Through these Python automated office cases, we can see the great potential of Python in improving office efficiency.
This is the end of this article about 10 Python automation office scripts. For more related Python automation office content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!