There is a popular question on Zhihu: Will Python become a common programming tool for public offices in the future?
In the programming world, Python is already a veritable internet celebrity. Once, a graduate student studying Chinese language asked me how to learn Python, because their course paper needed to use text analysis and use Python to run data. I told him that if you read the grammar in two days, you can start working. If you don’t know how, you can look up the information. 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 Java and C++, providing non-programmers with the possibility of working 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 spread across all walks of life.
Let’s take a dozen common examples of office automation, and 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 pandas, xlwings, openpyxl and other packages to perform operations such as additions, deletions, modifications, and format adjustments on Excel. You can even use Python functions to analyze excel data.
Read excel table
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 plot to excel table
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. Many people have various needs for processing PDF, such as making PDF, getting text, getting pictures, getting tables, etc. There are packages in Python such as PyPDF, pdfplumber, ReportLab, and PyMuPDF that can easily meet these needs.
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 table
# Extract pdf table import pdfplumber with pdfplumber.open("example.pdf") as pdf: page01 = pdf.pages[0] #Specify 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 automated transmission of emails, which is very convenient.
import smtplib import email # Responsible for gathering multiple objects from email.mime.multipart import MIMEMultipart from email.header import Header # SMTP server, 163 mailbox is used here mail_host = "smtp.163.com" # Sender's email address mail_sender = "******@163.com" # Email authorization code. Note that this is not the email password. How to obtain the email authorization code? Please see the tutorial at the end 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 text objects 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 information interacting with the SMTP server stp.set_debuglevel(1) # Log in to the email, pass parameter 1: email address, parameter 2: email authorization code stp.login(mail_sender,mail_license) #Send an email, passing parameter 1: sender’s email address, parameter 2: recipient’s email address, parameter 3: change the email content format to str stp.sendmail(mail_sender, mail_receivers, mm.as_string()) print("Email sent successfully") #Close SMTP object stp.quit()
4. Python processing database
Database is our commonly used office application. There are various database driver interface packages in Python to support addition, deletion, modification, query, and operation and maintenance management of the database. 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 for MySQL
import pymysql #Open 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() # Use the execute() method to execute SQL queries cursor.execute("SELECT VERSION()") # Use the fetchone() method to obtain a single piece of data. data = cursor.fetchone() print ("Database version : %s " % data) #Close database connection db.close()
5. Python processes batch files
For many office scenarios, batch processing of files has always been a dirty job, and Python can help you get out of the misery. There are many packages in Python that handle system files, such as sys, os, shutil, glob, path.py, etc.
Batch delete folders with the same name in different folders
import os,shutil importsys 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 suffixes 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 a dot.):') new_suffix = input('Please enter the suffix you want to change (need to add a 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 control mouse
This is the need of many people to realize automatic control of the mouse and 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 functions and test source code
# Get 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()#Double left click except KeyboardInterrupt: print("\ ")
7. Python control keyboard
Similarly, Python can also control the keyboard through pyautogui.
Keyboard writing
import pyautogui #typewrite() cannot input Chinese content. If a mixture of Chinese and English is used, only English content can be input. #interval sets 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 and manual operation is required.
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 operate 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") #Writing a compressed file will overwrite the original content in the compressed file. 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") ##Extract 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
Python crawler should be the most popular feature and the main reason why Python enthusiasts get involved.
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 parse the captured web page content, which is called parsing package.
Crawl Baidu homepage 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 images from urllib.request import urlretrieve #Request to get HTML html = urlopen("http://www.baidu.com/") # Use BeautifulSoup to parse html obj = bf(html.read(),'html.parser') # Extract the title from the tags head and title title = obj.head.title # Extract only 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'] # Use urlretrieve to download images urlretrieve(logo_url, 'logo.png')
10. Python processing of image charts
Image processing and chart visualization involve image processing, which is also the strength of Python. Nowadays, Python is also used in cutting-edge fields such as image recognition and computer vision.
Packages for processing images in Python include scikit Image, PIL, OpenCV, etc. Packages for processing charts include matplotlib, plotly, seaborn, etc.
Make images black and white
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 and help more people in need.
Finally:
I have prepared some very systematic Python materials. In addition to providing you with a clear and painless learning path, I have also selected the most practical learning resources and a huge library of mainstream crawler cases< /strong>. After a short period of study, you will be able to master the crawler skill well and obtain the data you want. Friends who need it can scan the QR code at the end of the article to obtain it.
01 is specially designed for 0 basic settings, and even beginners can learn it easily
We have interspersed all the knowledge points of Python into the comics.
In the Python mini-class, you can learn knowledge points through comics, and difficult professional knowledge becomes interesting and easy to understand in an instant.
Just like the protagonist of the comic, you travel through the plot, pass the levels, and complete the learning of knowledge unknowingly.
02 No need to download the installation package yourself, detailed installation tutorials are provided
03 Plan detailed learning routes and provide learning videos
04 Provide practical information to better consolidate knowledge
05 Provide interview information and side job information to facilitate better employment
This complete version of Python learning materials has been uploaded to CSDN. If you need it, you can scan the official QR code of csdn below or click on the WeChat card under the homepage and article to get the method. [Guaranteed 100% free] strong>