Top 10 Scenarios for Python to Handle Office Automation

There is a popular question on Zhihu, will Python become a common programming tool for public office in the future?

In the programming world, Python is already a veritable Internet celebrity. A graduate student who studied Chinese language once asked me how to learn Python, because they need to use text analysis in their course papers and use Python to run data. I told him that after two days of reading grammar, you can get started, and if you don’t know how to do it, you can check the information again. Later, this classmate used Python to complete the paper data in half a month.

Therefore, the biggest advantage of Python is that it is easy to learn, and the threshold is much lower than that of Java and C++, which provides the possibility for non-programmers to work with code. Of course, Python can become a popular programming tool, not only because it is easy to learn, but also because Python has thousands of toolkits in all walks of life.

To give more than 10 common examples of office automation, Python can handle them efficiently.

?

If you need Python office learning documents, you can reply [c] in the background to receive the packaged files.

?

1. Python processing Excel data

You can use packages such as pandas, xlwings, and openpyxl to perform operations such as addition, deletion, modification, and format adjustment on Excel, and you can even use Python functions to analyze excel data.

read excel form

import xlwings as xw
wb = xw.Book() # this will create a new workbook
wb = xw.Book('FileName.xlsx') # connect to a file that is open or in the current working directory
wb = xw.Book(r'C:\path\to\file.xlsx') # on Windows: use raw strings to escape backslashes

Write matplotlib plots to excel sheets

import matplotlib.pyplot as plt
import xlwings as xw

fig = plt. figure()
plt.plot([1, 2, 3])

sheet = xw.Book().sheets[0]
sheet.pictures.add(fig, name='MyPlot', update=True)

2. Python processing PDF text

PDF is almost the most common text format, and many people have various needs for processing PDF, such as making PDF, getting text, getting pictures, getting tables, etc. There are packages such as PyPDF, pdfplumber, ReportLab, and PyMuPDF in Python that can easily meet these requirements.

Extract PDF text

import PyPDF2

pdfFile = open('example.pdf','rb')
pdfReader = PyPDF2. PdfFileReader(pdfFile)
print(pdfReader. numPages)
page = pdfReader. getPage(0)
print(page. extractText())
pdfFile. close()

Extract PDF forms

# extract pdf form
import pdfplumber
with pdfplumber.open("example.pdf") as pdf:
    page01 = pdf.pages[0] #Specify the page number
    table1 = page01.extract_table()#Extract a single table
    # table2 = page01.extract_tables()#Extract multiple tables
    print(table1)

3. Python processing Email

In Python, you can use smtplib with the email library to realize the automatic transmission of emails, which is very convenient.

import smtplib
import email

# Responsible for grouping multiple objects together
from email.mime.multipart import MIMEMultipart
from email.header import Header

# SMTP server, use 163 mailboxes here
mail_host = "smtp.163.com"
# sender email
mail_sender = "******@163.com"
# Email authorization code, note that this is not the email password, how to get the email authorization code, please see the last tutorial of this article
mail_license = "********"
# Recipient email address, can be multiple recipients
mail_receivers = ["******@qq.com","******@outlook.com"]

mm = MIMEMultipart('related')
# Email body content
body_content = """Hello, this is a test email!"""
# Construct text, parameter 1: text content, parameter 2: text format, parameter 3: encoding method
message_text = MIMEText(body_content,"plain","utf-8")
# Add a text object to the MIMEMultipart object
mm. attach(message_text)

# create SMTP object
stp = smtplib.SMTP()
# Set the domain name and port of the sender's mailbox, the port address is 25
stp. connect(mail_host, 25)
# set_debuglevel(1) can print out all the information interacted with the SMTP server
stp.set_debuglevel(1)
# Log in to the mailbox, pass parameter 1: email address, parameter 2: email authorization code
stp.login(mail_sender, mail_license)
# Send email, pass parameter 1: sender email address, parameter 2: recipient email address, parameter 3: change the format of email content to str
stp. sendmail(mail_sender, mail_receivers, mm. as_string())
print("Mail sent successfully")
# Close the SMTP object
stp. quit()

4, Python processing database

The database is our commonly used office application. There are various database driver interface packages in Python, which support the addition, deletion, modification and query of the database, as well as operation and maintenance management. For example, the pymysql package corresponds to MySQL, the psycopg2 package corresponds to PostgreSQL, the pymssql package corresponds to sqlserver, the cxoracle package corresponds to Oracle, the PyMongo package corresponds to MongoDB, and so on.

Connection query to MySQL

import pymysql

# Open the database connection
db = pymysql.connect(host='localhost',
                     user='testuser',
                     password='test123',
                     database='TESTDB')
# Use the cursor() method to create a cursor object cursor
cursor = db. cursor()
# Execute SQL query using execute() method
cursor. execute("SELECT VERSION()")
# Use the fetchone() method to get a single piece of data.
data = cursor. fetchone()
print ("Database version : %s " % data)
# Close the database connection
db. close()

5. Python processes batch files

For many office scenarios, batch processing files has always been a dirty job, and Python can help you out of the sea of suffering. There are many packages in Python that deal with system files, such as sys, os, shutil, glob, path.py, and so on.

Batch delete folders with the same name under different folders

import os, shutil
import sys
import numpy as np

def arrange_file(dir_path0):
  for dirpath, dirnames, filenames in os. walk(dir_path0):
    if 'my_result' in dirpath:
      # print(dirpath)
      shutil.rmtree(dirpath)

Modify file extensions in batches

import os

def file_rename():
    path = input("Please enter the directory you need to modify (format such as'F:\test'):")
    old_suffix = input('Please enter the suffix you need to modify (need to add dot.):')
    new_suffix = input('Please enter the suffix you want to change to (need to add dot.):')
    file_list = os.listdir(path)
    for file in file_list:
        old_dir = os.path.join(path, file)
        print('current file:', file)
        if os.path.isdir(old_dir):
            continue
        if old_suffix != os.path.splitext(file)[1]:
            continue
        filename = os.path.splitext(file)[0]
        new_dir = os.path.join(path, filename + new_suffix)
        os.rename(old_dir, new_dir)

if __name__ == '__main__':
    file_rename()

6. Python controls the mouse

This is the demand of many people, to realize the automatic control of the mouse, and to do some assembly line work, such as software testing.

Python has a pyautogui library that can control your mouse arbitrarily.

Control mouse left click/right click/double click function and test source code

# Get the mouse position
import pyautogui as pg

try:
    while True:
        x, y = pg. position()
        print(str(x) + " " + str(y)) #output mouse position

        if 1746 < x < 1800 and 2 < y < 33:
            pg.click()#Left click
        if 1200 < x < 1270 and 600 < y < 620:
            pg.click(button='right')#right click
        if 1646 < x < 1700 and 2 < y < 33:
            pg.doubleClick()#Left double click

except KeyboardInterrupt:
    print("\\
")

7. Python controls the keyboard

Similarly, Python can also control the keyboard through pyautogui.

keyboard write

import pyautogui
#typewrite() cannot input Chinese content, and only English can be input if Chinese and English are mixed
#interval set the text input speed, the default value is 0
pyautogui.typewrite('Hello, world!', interval=0.5)

8. Python compressed file

Compressing files is a common operation in the office. Generally, compression software is used for compression, which requires manual operation.

There are many packages in Python that support file compression, allowing you to automatically compress or decompress local files, or package analysis results in memory. For example, zipfile, zlib, tarfile, etc. can realize operations on compressed file formats such as .zip, .rar, and .7z.

Compressed file

import zipfile
try:
  with zipfile.ZipFile("c://test.zip",mode="w") as f:
    f.write("c://test.txt") #Write into the compressed file, the original content in the compressed file will be overwritten
except Exception as e:
    print("The type of the exception object is: %s"%type(e))
    print("The content of the exception object is: %s"%e)
finally:
    f. close()

unzip files

import zipfile
try:
  with zipfile.ZipFile("c://test.zip",mode="a") as f:
     f.extractall("c://",pwd=b"root") ##Decompress the file to the specified directory, the decompression password is root
except Exception as e:
     print("The type of the exception object is: %s"%type(e))
     print("The content of the exception object is: %s"%e)
finally:
     f. close()

9. Python crawls network data

The python crawler should be the most popular function, and it is also the main reason for the majority of Python lovers to enter the pit.

There are many packages in Python that support crawlers, and crawler packages are divided into two types: crawling and parsing.

For example, requests and urllib are network data request tools, that is, capture packages; xpath, re, and bs4 will analyze the content of the captured web pages, which are called analysis packages.

Crawl Baidu home page pictures and save them locally

# import urlopen
from urllib.request import urlopen
# import BeautifulSoup
from bs4 import BeautifulSoup as bf
# Import the urlretrieve function for downloading pictures
from urllib.request import urlretrieve
# Request to get HTML
html = urlopen("http://www.baidu.com/")
# Parse html with BeautifulSoup
obj = bf(html.read(),'html.parser')
# Extract the title from the tags head and title
title = obj.head.title
# Only extract the information of the logo image
logo_pic_info = obj.find_all('img',class_="index-logo-src")
# Extract the link of the logo image
logo_url = "https:" + logo_pic_info[0]['src']
# Download images using urlretrieve
urlretrieve(logo_url, 'logo.png')

10. Python processing image chart

Image processing and chart visualization involve image processing, which is also the strength of Python. Now Python is also used in cutting-edge fields such as image recognition and computer vision.

The packages for processing images in Python include scikit Image, PIL, OpenCV, etc., and the packages for processing charts include matplotlib, plotly, seaborn, etc.

Black and white the image

from PIL import Image
from PIL import ImageEnhance

img_main = Image.open(u'E:/login1.png')
img_main = img_main. convert('L')
threshold1 = 138
table1 = []
for i in range(256):
  if i <threshold1:
    table1.append(0)
  else:
    table1.append(1)
img_main = img_main. point(table1, "1")
img_main.save(u'E:/login3.png')

Generate Statistical Charts

import numpy as np
import matplotlib.pyplot as plt

N = 5
menMeans = (20, 35, 30, 35, 27)
womenMeans = (25, 32, 34, 20, 25)
menStd = (2, 3, 4, 1, 2)
womenStd = (3, 5, 2, 3, 3)
ind = np.arange(N) # the x locations for the groups
width = 0.35 # the width of the bars: can also be len(x) sequence

p1 = plt.bar(ind, menMeans, width, yerr=menStd)
p2 = plt.bar(ind, womenMeans, width,
             bottom=menMeans, yerr=womenStd)

plt.ylabel('Scores')
plt.title('Scores by group and gender')
plt.xticks(ind, ('G1', 'G2', 'G3', 'G4', 'G5'))
plt.yticks(np.arange(0, 81, 10))
plt.legend((p1[0], p2[0]), ('Men', 'Women'))

plt. show()

Summary

In short, Python will become a popular programming language, helping more people in need

Python experience sharing

It is good to learn Python whether it is employment data analysis or doing side jobs to make money, but you still need a learning plan to learn Python. Finally, everyone will share a full set of Python learning materials to help those who want to learn Python!

Python learning route

Here we sort out the commonly used technical points of Python, and summarize the knowledge points in various fields. You can find the corresponding learning resources according to the above knowledge points.

Learning software

Python commonly used development software will save you a lot of time.

Learning video

To learn programming, you must watch a lot of videos. Only by combining books and videos can you get twice the result with half the effort.

100 practice questions

Actual case

Optical theory is useless. Learning programming should not be done on paper, but by hands-on practice, and apply the knowledge you have learned to practice.

Finally, I wish everyone progress every day! !

The above full version of the full set of Python learning materials has been uploaded to the official CSDN. If you need it, you can directly scan the QR code of the CSDN official certification below on WeChat to get it for free [guaranteed 100% free].