程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
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 There is a surprise at the end of the article

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 !")

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()

Office automation Click to receive books and materials Tencent documents - Online document https://docs.qq.com/doc/DT0hJTWFkd3pGbmFU


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