Python automated office

1. Basic operation

Get the current py file name

import os
file = 'test.py'
fname = os.path.basename(file)
print(fname)
#Through the basename() method, we can remove the path and get only the file name
#If the path fails to recognize the error (the problem of escape), add r before the path. (file = r'C:\Users\15205\PycharmProjects\pachong\xpath\test.py')
file1 = r'C:\Users\15205\PycharmProjects\pachong\xpath\test1.py'
fname1 = os.path.basename(file1)
print(fname1)	#Only the file name is displayed, and the path is not displayed
# os.path.abspath(filename) gets the complete absolute path with file name

Gets the working directory of the current py file

import os	#Import python built-in function modules
path = os.getcwd()	#Using the method of obtaining the directory, you do not need to pass in parameters, and assign the return value of the method to the path variable
print(path)	#Print out the value of the variable and get the working directory
# os. path. The dirname () method obtains only the parent directory path through the full path with file name, which is the same as OS path. Basename()

Get file size

import os
fname = 'test.py'	#Define file name variable
size = os.path.getsize(fname)	#Call the getsize() method in the path of the sub module of the os module
print(size)		#The returned file size is bytes

Splicing of file paths (full path with file name)

import os
fname = 'test.py'
fdir = os.getcwd()
fpath = os.path.join(fdir,fname)
print(fpath)
#This is just a splicing of file names and paths, and does not necessarily require the existence of file objects

Determine whether the file exists

#Judge whether the file exists, True/False
import os
file = r'C:\Users\15205\PycharmProjects\pachong\xpath\test.py'
fname = os.path.exists(file)
print(fname)

Determine whether it is a file or directory

#Determine whether it is a file
import os
file = r'C:\Users\15205\PycharmProjects\pachong\xpath\test.py'
flag = os.path.isfile(file)
print(flag)

#Determine whether it is a directory
import os
filedir = r'C:\Users\15205\PycharmProjects\pachong\xpath'
flag = os.path.isdir(filedir)
print(flag)

How to create and delete directories

import os
import time
path1 = 'work'
if os.path.exists(path1):
	print("The directory already exists")
else:
	os.mkdir(path1)
time.sleep(6)
os.rmdir(path1)
print('Directory deleted successfully')

How to delete a specified file object

import os
fname = 'car.py'
if os.path.exists(fname):
	os.remove(fname)
else:
	print('The current file does not exist')

Gets the manifest information of the specified directory

import os
fdir = os.getcwd()
infos = os.listdir(dir)
print(infos)

Calculate the total size of all files in the specified directory

import osfdir = os.getcwd()flist = os.listdir(fdir)fsize = 0for item in flist:    if os.path.isfile(item):        size = os.path.getize(item)        fsize += sizeprint(fsize)# Unit byte

2. File reading and writing

How to open a file

file = open('le1.py', 'r', encoding='utf-8')	# Python 3 built-in method to open file object
print(file)
# First parameter: file name
# Second parameter: operation mode
# The third parameter: encoding format

Read all data from the file

file = open('le1.py', 'r', encoding='utf-8')
data = file.read()
print(data)
# After reading the file content, the file object should be released, that is, the operating system resources occupied by it should be released
file.close()

Context manager object

with open('test.py', 'r', encoding='utf-8') as file:
    data = file.read()
    print(data)
# This method will automatically release the occupied file resources without file close()

File line data reading

file = open('test.py', 'r', encoding='utf-8')
# Initial value of row data
line = 1
while line != '':	# Row data is not empty
    line = file.readline()
    print(line.strip())	# strip() to remove auto wrap

Read all rows of data at once

file = open('test.py', 'r', encoding='utf-8')
lines = file.readlines()	# Different from readline
# print(type(lines))	# [first row of data, second row of data, third row of data...]
# print(lines)
for line in lines:
    print(line.strip())

How to write data

content = 'i love you'	# Default string type. If the type is wrong, an error will be reported fname = 'stroy.txt'with open(fname, 'w', encoding='utf-8') as file:    file.write(content)# file.write(str(content))	# Cast to string type

How to batch write data

content = ['i love you\n', 'what?\n', 'hehe\n']fname = 'story.txt'with open(fname, 'w', encoding='utf-8') as file:    file.writelines(content)

How to append data

# The 'w' operation type used to write data above will overwrite the data. The following example shows how to append data ('a ') content =' hello world 'fname =' stroy txt'with open(fname, 'a', encoding='utf-8') as file:    file. write(str(content))

3. File compression

decompression

import zipfile			# Introduction of compression module, built-in module, no installation required zf = zipfile.ZipFile('stroy.zip', 'r')	# Read compressed files through Manager zf.extractall('outf')	# Specify the directory where the compressed files are stored after decompression. Here, create a directory in the current directory outf catalogue zf.close()				# Release resource object

How to read compressed file information

// Method 1: import zipfilezf = zipfile ZipFile('stroy.zip', 'r')zlist = zf. namelist() 	#  Read the name of all compressed files print(zlist) 			#  Print the file list / / method 2: import zipfilezf = zipfile ZipFile('stroy.zip', 'r')zlist = zf. infolist() 	#  Not only the file name, but also the file size for info in zlist: Print (info.filename, info.file_size)

Compression of directory resources

import zipfileimport globimport oszf = zipfile.ZipFile('inner.zip', 'w')path1 = glob.glob('outf/*')		# You can use the absolute path for name in path1: Print (name) ZF write(name, os.path.basename(name))

4. How to realize automatic classification of documents

premise

There are many different types of resource files in the same directory.

step

  • classification
  • Create catalog
  • Move file resources

code implementation

import osimport shutil	# stay os Based on the module, it is encapsulated again, and the function is more powerful# Existence path of source file src_dir = r'C:\Users\15205\PycharmProjects\pachong\xpath\resource'# Storage path of classified resources dest_dir = r'C:\Users\15205\PycharmProjects\pachong\xpath\classify'# Judge whether the classification directory exists. If it does not exist, create it if not os.path.exists(dest_dir):    os.mkdir(dest_dir)# Analyze the source directory and judge the resource type files = os.listdir(src_dir)for item in files:    # print(item)ļ¼ŒOutput all file names    # Processing path    src_path = os.path.join(src_dir, item)    # Judgment state    if os.path.isfile(src_path):        # If it is a file, enter the code block        # Determine the type of file resource        ndir = item.split('.')[-1]        desc_path = os.path.join(dest_dir, ndir)        # Create classification        if not os.path.exists(desc_path):            # If the category directory does not exist, create OS mkdir(desc_path)        shutil. move(src_path, desc_path)

5. How to find files quickly

Direct search

from os import path

while True:
    # Specify the lookup directory
    dir = input("Which directory do you want to find:").strip()
    # judge
    if path.exists(dir) and path.isdir(dir):
        break
    else:
		print("Incorrect input information!")
        
# Determine the search target (file, directory)
dist = input("Please enter the name of the target you want to find:")
# Splicing processing
rpath = path.join(dir, dist)
# Judge whether it exists
if path.exists(rpath):
    print("Search succeeded")
else:
    print("Search failed")

# This program can only find the files in the specified directory, so how to find the files in the directory in the specified directory?

6. Automatically clean up duplicate files

step

  • Set the specified directory and determine the directory structure of the file

  • Analyze and compare all file resources in this directory

  • Delete and clean up redundant file resources

code implementation

from pathlib import Path
from filecmp import cmp

dir = r'C:\Users\15205\PycharmProjects\pachong\xpath\test'
path1 = Path(dir)
if not path1.exists():
    print('The specified directory does not exist. Please reconfigure the directory')
# test exists. Analyze all file resources in the directory
plist = list(path1.glob('*'))
print(plist)
if len(plist) == 0:
    print('The specified directory is empty and has no resources')
else:
    # The documents need to be analyzed and compared
    flist = [file for file in plist if file.is_file()]
    for m in flist:
        for n in flist:
            if m.exists() and n.exists() and m!=n:
                # Analysis and comparison of documents
                if cmp(m, n):
                    # Indicates that there are redundant resources. Delete the redundant resources
                    m.unlink()
    print('Duplicate files have been cleaned up. Thank you for using. Bye!')

7. Batch conversion of picture format

step

  • Wrapper path object

  • Loop through all pictures

  • Get each picture and convert the format

code implementation

from pathlib import Path# Install third-party libraries pip install pillowfrom PIL import Image# Define two paths dir1 = r'C:\Users\15205\PycharmProjects\pachong\xpath\test'dir2 = r'C:\Users\15205\PycharmProjects\pachong\xpath\work'# structure Path Instance object path1 = Path(dir1)path2 = Path(dir2)# judge work Does the directory exist if not path2.exists():    path2.mkdir()# ergodic test Get all file objects from directory plist = list(path1.glob('*.jpg'))print(plist)# obtain list Each file object in the for file in plist:	# Convert picture format    rpath = (path2/file.name).with_suffix('.png')    # Save file object    # First open the original picture, and then save the new picture image open(file). Save (rpath) print ('All pictures have been converted to the new format *. png ')

8. Pictures are classified by time

from pathlib import Path
import os
import shutil
import time

dir1 = r'C:\Users\zhou chang\PycharmProjects\pachong\Xpath\test'
dir2 = r'C:\Users\zhou chang\PycharmProjects\pachong\Xpath\work'
# Wrapper, creating instance objects
path1 = Path(dir1)
path2 = Path(dir2)
# Judge the work directory. If it does not exist, create it
if not path2.exists():
    path2.mkdir()
plist = list(path1.glob('*'))
print(plist)
for p in plist:
    with open(p, 'rb') as file:
        info = os.stat(p)
        # print(info)
        t = str(time.localtime(info.st_ctime).tm_year) + str(time.localtime(info.st_ctime).tm_mon)
        # print(t)
        rpath = path2/t
        if not rpath.exists():
            rpath.mkdir()
        # Move to new directory by time
        # Processing the path of files in the new directory
        rfile = rpath/p.name
    # move file
    p.replace(rfile)       

9. Version variance control

step

  • Understand version information

  • View version information

  • version control

Get python version information

import sysinfos = sys.version_infoprint(infos)info = infos[0]print(info)if info < 3:    print('python The version is lower, and the current version is %s.%s.%s' %(infos[0], infos[1], infos[2]))    print('Exit program execution')    exit()else:    print('Continue execution python3 Version program')

10. Module installation control

thinking

  • Module not installed error

  • Analyze the cause mechanism

  • Backward solution

  • Program automatic detection control

scene

import pyecharts	# An error is reported during operation, indicating that there is no module

Reason: the third-party library is not installed
 Solution:
Method 1: pip install ***
Method 2: pycharm Quick installation

The program automatically detects the missing library and executes the installation

import os
cmd = 'pip install requests'
# pip38 install requests
os.system(cmd)

Bring judgment and then match and download

import os
try:
    import pyecharts
except Exception as e:
    print('It is detected that the feature library is not installed. Start downloading and installing')
    cmd = 'pip install pyecharts'
    os.system(cmd)

11. Automatically start mysql service

thinking

  • mysql connection error

  • Exception capture

  • Auto start service

  • Connect database

Code implementation (automatically start mysql service by opening a sub process)

#! /usr/bin/env python
# -*- coding:utf-8 -*-
# Install mysql version 8.0 for windows 10
# pip install pymysql
import pymysql
import subprocess	# Built in module

# Database information configuration
host = '127.0.0.1'
dbname = 'office'
user= 'root'
paw = 'lhn115023808'

# Connect database
try:
	pymysql.connect(host=host, port=3306, db=dbname, user=user, passwd=paw)
except Exception as e:
	# Judge the exception type
    if e.__class__.__name__=='OperationalError':
        # mysql service is not enabled
        cmd = 'net start mysql80'	# mysql8. The service name of version 0 is mysql80
    	# Start the subprocess and mysql80 service
        subprocess.call(cmd)
        # Connect to mysql database
        pymysql.connect(host=host, port=3306, db=dbname, user=user, passwd=paw
        print('Continue to perform normally in the future')	# Simulation process
        # You may see that the output is garbled. Change the pycharm encoding format to GBK            

12. Automatic timing program

thinking

  • Scene introduction

  • Sleep function

  • timer

  • Scheduler

Scenario 1: repeat the task at regular intervals

Method 1: the thread will block

from datetime import datetime
import time

# Design function function (print out time information)
def print_time(count):
    # Tasks done
    print(datetime.now().strftime("%Y:%m:%d %H:%M:%S"))		# Year month day hour minute second
    time.sleep(count)
    print('interval%s Printout' % count)
    
# Repeat three times
for i in range(3):
    print_time(6)

Method 2: multithreading

from datetime import datetime
from threading import Timer

def print_time():
    # Tasks done
    print(datetime.now().strftime("%Y:%m:%d %H:%M:%S"))		# Year month day hour minute second
    
for i in range(3):
    # Parameter: time interval 6 seconds
    inc = 6
    # Create timer object
    t = Timer(inc, print_time)	# Timer Objects 
    t.start()
    print('I'm not syncing')

Scenario 2: repeat the task at fixed time and fixed point

Design of fixed-point task

# pip install apscheduler
from datetime import datetime
from apscheduler.schedulers.blocking import BlockingScheduler

# Custom execution tasks
def job():
    print(datetime.now().strftime("%Y:%m:%d %H:%M:%S"))
# Create scheduler object
scheduler = BlockingScheduler()
# Assign tasks
scheduler.add_job(func=job, trigger='cron', day_of_week='0-6', hour='17', minute='40', second='00')
# Start scheduler
scheduler.start()

13. Program packaging function

thinking

  • The meaning of program packaging: package the program and environment together, and it can be executed without python environment.

  • Library installation: install the third-party library pip install pyinstaller

  • Packaging programming

  • Packaging parameter configuration

Write a simple program and demonstrate the packaging method

Design simple program

#! /usr/bin/env python
# -*- coding:utf-8 -*-

# pip install pyinstaller
# python3. pyinstaller installed on version 8 and above may not automatically add environment variables, or it cannot be used
# My solution is to pyinstaller Exe copy to the current directory

import time

def job():
    print('Packaging function demonstration')

# Writer entry function
if __name__=='__main__':
    job()
    • Packaging method 1: interactive interface packaging

Switch to the path of py file in cmd

[the external chain image transfer fails. The source station may have anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-e9biqnkj-1643119894604) (C: \ users \ 15205 \ appdata \ roaming \ typora user images \ image-2022012422532077. PNG)]

Execute pyinstaller - f run py

[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-nwmpa6cq-1643119894605) (C: \ users \ 15205 \ appdata \ roaming \ typora \ typora user images \ image-20220124230527323. PNG)]

[the external chain image transfer fails, and the source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-ju1gfiyx-1643119894606) (C: \ users \ 15205 \ appdata \ roaming \ typora user images \ image-20220124230910450. PNG)]

Click this run Exe will flash by. In fact, it has been executed.

Modify the code, repackage and verify it.

import time

def job():
    print('Packaging function demonstration')
    time.sleep(20)	# Sleep for 20 seconds

# Write program entry function
if __name__=='__main__':
    job()

pyinstaller -F run.py

The new bag will cover the old one.

[the external chain image transfer fails. The source station may have anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-qajpe7iu-1643119894606) (C: \ users \ 15205 \ appdata \ roaming \ typora user images \ image-20220124213126983. PNG)]

As you can see, it is being implemented.

    • Packaging method 2: manual packaging and writing into the program

Automatic program packaging

import os

def main():
    cmd = 'pyinstaller -F run.py'
    os.system(cmd)

if __name__ == '__main__':
    main()

Execute the script and view the output and results.

[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-5n2lhh9g-1643119894607) (C: \ users \ 15205 \ appdata \ roaming \ typora user images \ image-2022012423212593. PNG)]

[the external chain image transfer fails. The source station may have anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-i7kuqwyw-1643119894607) (C: \ users \ 15205 \ appdata \ roaming \ typora user images \ image-20220124232128294. PNG)]

[the external chain image transfer fails, and the source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-qgvowij5-1643119894607) (C: \ users \ 15205 \ appdata \ roaming \ typora \ typora user images \ image-20220124232211529. PNG)]

14. Send SMS

thinking

  • Third party platform

  • Registered account

  • Save id, token

  • Design script program

Concrete implementation

Open web page: https://www.yuntongxun.com , registered account number.

accid: 8aaf07087e7b9872017e8cb6970002a4
acctoken: ec070071d417407591d44b66e79f97b3
appid: 8aaf07087e7b9872017e8cb6980702ab

[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-9i9qo9mz-1643119894608) (C: \ users \ 15205 \ appdata \ roaming \ typora user images \ image-20220124233521217. PNG)]

[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-2ru5rocp-1643119894608) (C: \ users \ 15205 \ appdata \ roaming \ typora user images \ image-20220124235403256. PNG)]

Programming

# pip install ronglian-sms-sdk

# Guided package process
from ronglian_sms_sdk import SmsSDK

# Parameter configuration
accId = '8aaf07087e7b9872017e8cb6970002a4'
accToken = 'ec070071d417407591d44b66e79f97b3'
appId = '8aaf07087e7b9872017e8cb6980702ab'

# Design function (send SMS)
# Two parameters: mobile phone number; Verification code and expiration time.
def send_sms(phone, datas):
    # Create instanced object
    sdk = SmsSDK(accId, accToken, appId)
    tid = '1'
    # Send SMS
    resp = sdk.sendMessage(tid, phone, datas)
    return resp

if __name__=='__main__':
    # configuration parameter
    phone = '***********'
    datas = ('123456', '3')
    send_sms(phone, datas)

If the operation is normal, your mobile phone should receive a verification code SMS with a verification code of 123456.

15. PDF file

15.1 PDF file content acquisition

thinking

  • PyPDF2 Library (pip install PyPDF2 # doesn't support Chinese very well)

  • File object

  • Get file pages

  • Get file data

Text file: processing of text string

file = open('test.py', 'r', encoding='utf-8')
print(type(file))
result = file.read()
print(result)

PDF file: processing of binary format data

import PyPDF2

# Get file object
file = open('show.pdf', 'rb')
pdfrd = PyPDF2.PdfFileReader(file)

# Object Gets the number of pages for the property name
pages = pdfrd.numPages
# print(pages)
index = int(input('Please enter the corresponding number of pages(Start from 0): '))
if index < pages:
    page = pdfrd.getPage(index)
    result = page.extractText()
    print(result)
else:
    print('The number of pages does not exist, please confirm!')

15.2 PDF encryption check

thinking

  • Encryption status

  • PDF decryption

Judge whether the pdf file is encrypted

import PyPDF2

def is_pwd(fname):
    pdfobj = open(fname, 'rb')
    pdfrd = PyPDF2.PdfFileReader(pdfobj)
    # Start judging properties (encryption?) Object attribute
    if pdfrd.isEncrypted:
        print(f'{fname} Is the encryption status')
    else:
        print(f'{fname} No encryption')

if __name__ == "__main__":
    fname = 'show.pdf'
    is_pwd(fname)

Extraction of encrypted pdf file data

import PyPDF2

def de_pwd(fname, pwd):
    pdfobj = open(fname, 'rb')
    pdfrd = PyPDF2.PdfFileReader(pdfobj)
    if pdfrd.isEncrypted:
        print('Start decryption processing:')
        result = pdfrd.decrypt(pwd)
        if result:
            page = pdfrd.getPage(1)
            content = page.extractText()
            print(content)
        else:
            print('Sorry, decryption failed!')
    else:
        print(f'{fname} It is not encrypted')

if __name__=='__main__':
    fname = 'show.pdf'
    pwd = '*******'
    de_pwd(fname, pwd)
    
// If it cannot be decrypted, check whether the password is wrong. Another possibility is the problem of PyPDF2 module. This module is very old and is about to give up maintenance. Refer to: https://blog.csdn.net/weixin_39278265/article/details/84799843

15.3 PDF transfer

thinking

  • Single page transfer

  • Multi page transfer

  • Encrypted transfer

Transfer the contents of the second page of a PDF to a new file

def single_page(fname, new_file, index):
    import PyPDF2
    # Open the original PDF file
    pobj = open(fname, 'rb')
    # Construct reader object
    pdfrd = PyPDF2.PdfFileReader(pobj)
    # Get the corresponding page object
    page = pdfrd.getPage(index)
    # Write data
    pdfwr = PyPDF2.PdfFileWriter()
    # Add page object
    pdfwr.addPage(page)
    # Create a new PDF file
    pdfout = open(new_file, 'wb')
    # The core step is to transfer pdf content
    pdfwr.write(pdfout)
    # Free file resources
    pobj.close()
    pdfout.close()

if __name__=="__main__":
    fname = 'old.pdf'
    new_file = 'new.pdf'
    index = 1
    single_page(fname, new_file, index)
    
// If an error is reported, it may be the problem of PyPDF2 module

All pages are transferred and encrypted

def all_page_encrypt(fname, new_file):
    import PyPDF2
    pobj = open(fname, 'rb')
    pdfrd = PyPDF2.PdfFileReader(pobj)
    pdfwr = PyPDF2.PdfFileWriter()
    pages = pdfrd.numPages
    for index in range(pages):
        pdfwr.addPage(pdfrd.getPage(index))
    pdfwr.encrypt('999999')
    pdfout = open(new_file, 'wb')
    pdfwr.write(pdfout)
    pobj.close()
    pdfout.close()

if __name__=="__main__":
    fname = 'old.pdf'
    new_file = 'new1.pdf'
    all_page_encrypt(fname, new_file)

Keywords: Python Back-end cloud computing

Added by djg_uk on Wed, 26 Jan 2022 05:57:12 +0200