1946, the world’s first universal computer “ENIAC” was born in the University of Pennsylvania; ENIAC covers 170 square meters, weighs 30 tons, consumes about 150 kilowatts of power and can perform 5,000 calculations per second. It is used by the U.S. Department of Defense for ballistic calculations.

At that time, computers were only used in special departments. Now has been more than 60 years, the human in the computer development process is further and further, the development of technology to make the price is cheaper, the volume is more convenient to carry, the computer has appeared in all walks of life. As mentioned in the 47th Statistical Report on The Development of Internet in China, by December 2020, the number of Internet users in China had reached 989 million, and the Internet penetration rate had reached 70.4%. Such a large number of users must have a huge commercial market, and the computer applications needed are becoming more and more diverse. So creating these computer applications requires programming.

In the computer, writing programs need to use computer programming languages, because of different types and targeted, there are hundreds of computer languages, that for the present increasingly complex office needs, in the end what language can improve our office efficiency? Now there’s an answer. The popular language is Python, and Python has a lot of third-party libraries, or libraries that are already implemented, just waiting for you to use them to do the customization you need; We only need to learn the basic Python syntax to improve our productivity in the office.

In this article, we will simulate solving 10 office needs and introduce you to the power of Python. Of course, readers can also get these 10 solutions directly from this article. The 10 office requirements addressed in this article are as follows:

On my first day at work, my boss asks me to retrieve my phone number from a pile of text messages. How do I do it?

On the second day of work, the leader asked me to store the phone number extracted on the first day into Excel. How did I solve the problem quickly?

On the third day of work, I was called to extract the mailbox from the text today. I was given one day, but I only started to extract information after playing for a long time.

On my fourth day, I was given a bunch of pictures and asked to add company watermarks.

On the fifth day of work, there were too many duplicate files in my former colleague’s computer, so my leader asked me to clean up duplicate files and simplify information.

On the sixth day, the leader told me to count the number of Chinese characters in this text.

On the seventh day, I helped the artist to generate two-dimensional code pictures.

On the eighth day of work, how to generate GIF pictures? I can do it easily.

On the ninth day of my work, the personnel asked me to translate an English document in a hurry, and I immediately agreed to come down.

On the tenth day of work, extract the audio information of video and get a promotion and raise!

First day at work

On your first day at work, your supervisor gives you a stack of text files and asks you to retrieve your cell phone number. If you are a regular office worker, you will search for information one by one in the text, but in the era of widespread computer, it is obviously a better choice to improve office efficiency and liberate yourself. So let Python give you a leg up on your career.

First of all, we can consider that the text file is the suffix TXT file, the first thing that this text file needs to be read; Py creates a Python file named day1.py and writes a function named get_str. The parameter is the path to the file to be read. This function returns the read.

Def get_str(path): f = open(path,encoding=" utF-8 ") data = f.read() f.lose () return dataCopy the code

Now that you have written the function to read the text content, you should then extract the phone number from the value that you read. The phone number is extracted using the re. To use re we need to use the re module; When the re module is introduced, call the findAll method of the RE module to read the phone number and return:

Def get_phone_number(STR): res = re.findall(r'(13\d{9}|14[5|7]\d{8}|15\d{9}|166{\d{8}|17[3|6|7]{\d{8}|18\d{9})', str) return resCopy the code

So the last step is to save the information. Create a function named save_res, pass in two parameters, respectively, the result of extracting the number and the path to save the file, then iterate over the result using write method, the function code is as follows:

Def save_file (res,save_path): save_file = open(save_path, 'w') for phone in res Save_file.write (phone) save_file.write('\n') save_file.write('\n') save_file.write('\n Number: '+ STR (len(res))) save_file.close() print(len(res))Copy the code

The last step is how to call the code you’ve created. Use input to receive two input values; One is the path of the target file to read, the other is the path of the file to save the result, and then call the function in sequence, the code is as follows:

Save_path =input(" Please enter the file path: ") save_path=input(" Please enter the file saving path: ") #read_str=get_str(path) res=get_phone_number(get_str(path)) save_res(res,save_path)Copy the code

At this point, we create a TXT file for the test, the file name and suffix is phone. TXT, the content is:

Zhang SAN: 15888888888 Li Si: 15888888888 Straw hat: 15888888888 Li Si: 15888888888 Willow leaf: 15888888888 Li Si: 15888888888 Straw hat: 15888888888 Li Si: 1588888888888 Willow leaf: 1588888888888 li Si: 1588888888888 Li Si: 15888888888 Willow leaves: 15888888888 Li Si: 15888888888 Willow Leaf: 15888888888Copy the code

Next, run the Python file day1 in DOS and enter the file storage path and save path. When the information is extracted, you will be prompted:

When you go to the saved file res.txt, you find that the phone number information has been extracted:

Wouldn’t it be nice to get your first day of work out of the way and share your script with your colleagues to raise your profile?

Second day of work

On the second day of work, you are newly assigned the task of saving the phone. TXT text of the first day in Excel. If the text contains tens of thousands of messages, it may take you a long time to complete this task manually, and there is a high probability of omission. Using Python for automation at this point will greatly reduce your operating time, and the probability of missing data is extremely low if the program is correct. Is Python good enough to function the next day? The answer, of course, is “Yes!” . Python has a third-party library called XLWT that automatically saves data to Excel files. Let’s see how this works.

First create a Python file called day2.py and introduce XLWT in the header:

import xlwt
Copy the code

Since the data we currently need is from day 1, we continue with the get_str function we used on day 1:

Def get_str(path): f = open(path,encoding=" utF-8 ") data = f.read() f.lose () return dataCopy the code

Next we create a function named save_excel that saves the file, sets the sheet name, sets the column name, and sets the column value. The save_excel function accepts four parameters save_PATH, sheetname, column_name_list, and content. First we create a Workbook object inside the function:

def save_excel(save_path,sheetname,column_name_list,read_list):
    workbook = xlwt.Workbook()
Copy the code

Add a sheet to the body of the function using add_sheet. The add_sheet function accepts a sheetname and uses the sheetName parameter as the sheetname value. The add_sheet function will return the sheet object created with the following code:

sheet1 = workbook.add_sheet(sheetname=sheetname)

Column_name_list After receiving the parameters, we can use the for loop to set the column_name_list on the sheet as passed:

for i in range(0,len(column_name_list)):
        sheet1.write(0,i,column_name_list[i])
Copy the code

In the above code, the first parameter of the write method is the sheet line, where 0 is the first line; Parameter I is the number of columns, because I is traversed from 0 to the length of the current column element, in this case, it is from 0 to the last element of the column_name_list, so it will start from the beginning column to the corresponding last column. Fill all column names into the header of the value sheet page.

Then it’s time to set the elements for the columns. We iterate over the read_list, which is the specific contents of these columns, such as name and phone number. Since each line of the original data will be separated by: as the name and phone number, for example, “Zhang SAN: 15888888888”, the read_list should be separated by the value and assigned with the same value as the column name. The code will not be described here:

I =1 for v in read_list: kval= v.splash (' : ') for j in range(0,len(kval)): sheet1.write(I +1,j,kval[j]) print(kval[j]) I = I +1Copy the code

Finally, call the save method with the Workbook object, passing the save address. Then the complete code of save_excel custom function is as follows:

Def save_excel(save_path,sheetname,column_name_list,read_list): workbook = xlwt.Workbook() sheet1 = workbook.add_sheet(sheetname=sheetname) for i in range(0,len(column_name_list)): Sheet1. write(0, I,column_name_list[I]) I =1 for v in read_list: kval= v.splash (' : ') for j in range(0,len(kval)): Sheet1. write(I +1,j,kval[j]) I = I +1 workbook.save(save_path) print(' save OK, read_list ')Copy the code

Sheetname, column name and the newline “\ N” delimiter for the raw data. The complete code is as follows:

Save_path =input(" Please input file path: ") save_path=input(" Please input file save path: ") sheet_name=input(" please input sheetname: ") column_name=input(" Please enter column names separated by commas: ") column_name_list=column_name.split(',') read_str=get_str(path) read_list=read_str.split('\n') save_excel(save_path,sheet_name,column_name_list,read_list)Copy the code

At this point, run the day2.py file. After entering the required content, a success message will appear:

You can then see the extracted information in the saved file:

Due to the limited space, the likes and comments more than ten ZA continue to update, no one to see the calculate!

How to obtain the source code:

① More than 3000 Python ebooks ②Python development environment installation tutorial ③Python400 set self-learning video ④ software development common vocabulary ⑤Python learning roadmap ⑤ project source code case sharing if you use it can be directly taken away in my QQ technical exchange group group number: 754370353 (pure technical exchange and resource sharing, no advertising) to take away by yourself click here to collect