程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[Python automated office] share several popular modules and suggest collecting them

編輯:Python

Today, I'd like to recommend some very useful in the field of office automation Python modular , It can help everyone improve efficiency in their work , Avoid repeating the mechanized operation process . Like to remember to collect 、 Focus on 、 give the thumbs-up

More technical exchanges , At the end of the article

Pathlib

When it comes to file system operation , I believe many people are still using Python In the middle of OS modular , By comparison ,Pathlib Modules have many advantages , Let's look at a few simple cases

Deleting and creating directories

For example, we can create and delete directories , The code is as follows

from pathlib import Path
currentPath = Path.cwd()
makePath = currentPath / 'pythonPractice'
makePath.mkdir()

So the same , The code for deleting the directory is

currentPath = Path.cwd()
delPath = currentPath / 'pythonPractice'
delPath.rmdir()

Get directory

For example, we want to get the path of the current directory , The code is as follows

currentPath = Path.cwd()
print(currentPath)

And the directory where the computer user is located

homePath = Path.home()
print(homePath)

Splicing of paths

For example, we want to splice the absolute path of the desktop , The code is as follows

Path(Path.home(), "Desktop")

It can also be

Path.join
path(Path.home(), "Desktop")

Directory file judgment

For the specified path , We can judge whether it is a folder and whether it is a file , The code is as follows

input_path = r" Specified path "
if Path(input_path ).exists():
if Path(input_path ).is_file():
print(" It's a file !")
elif Path(input_path ).is_dir():
print(" It's a folder !")
else:
print(" The path is wrong !")

glob

Python In the middle of glob The module is mainly used to find directories and files that meet specific rules , And return the search results to a list .

Because the module supports the use of regular wildcards to find , Therefore, it is particularly convenient to use , Let's take a look at a simple case ,

path1 = r".\[0-9].jpg"glob.glob(path1)

outut

['.\\1.jpg', '.\\2.jpg', '.\\3.jpg', ......]

Wildcards that are often used include

  • *: matching 0 One or more characters

  • **: Match all files 、 Catalog , Subdirectories and files in subdirectories

  • []: Match characters in the specified range , for example [1-9] The match is 1-9 Characters within

  • [!]: Matches characters that are not in the specified range

Let's look at a few more cases , The code is as follows

for fname in glob.glob("./*.py"):
print(fname)

The above code prints out all the files in the current directory py Postfix file , Let's take another look at the case

for fname in glob.glob("./file[!0-9].py"):
print(fname)

The above code is printed out with filename Beginning with a non numeric symbol py file .

PDF Turn into Word file

Finally, let's talk about how to achieve PDF Document conversion to Word The format of the document , The modules used are pdf2docx, We use first pip Command to install this module

pip install pdf2docx

Let's practice , The code is as follows

from pdf2docx import Converter
cv = Converter(r"pdf The specific path of the document ")
cv.convert("test.docx", start=0,end=None)
cv.close()

If it is a document with simple page elements ,pdf2docx Module processing is completely enough , But sometimes PDF Individual pages in the document are very colorful , convert to Word The format of the document will appear a little messy .

Finally, we can also convert for the specified number of pages , For example, it only operates on odd pages in the document , The code is as follows

from pdf2docx import Converter
cv = Converter(r"pdf The specific path of the document ")
cv.convert("test.docx", pages=[1, 3, 5, 7])
cv.close()

Recommended articles

  • Li Hongyi 《 machine learning 》 Mandarin Program (2022) coming

  • Some people made Mr. Wu Enda's machine learning and in-depth learning into a Chinese version

  • Addicted to , Recently, a large visual screen has been rolled out for the company ( Source code attached )

  • So elegant ,4 paragraph Python Automatic data analysis artifact is really fragrant

  • Combing for more than half a month , Well prepared 17 A map of knowledge and thinking , Let's talk about statistics this time

  • Year end summary :20 Visual large screen template , Direct application is really fragrant ( The source code is attached at the end of the article )

Technical communication

Welcome to reprint 、 Collection 、 Gain some praise and support ! data 、 The code can be obtained from me

At present, a technical exchange group has been opened , Group friends have exceeded 2000 people , The best way to add notes is : source + Interest direction , Easy to find like-minded friends

  • The way ①、 Send the following picture to wechat , Long press recognition , The background to reply : Add group ;
  • The way ②、 Add microsignals :dkl88191, remarks : come from CSDN
  • The way ③、 WeChat search official account :Python Learning and data mining , The background to reply : Add group


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved