Using python to batch modify word file names,

Source: Internet
Author: User

Using python to batch modify word file names,

Preface

Recently, I accidentally formatted the hard disk. Because the files were not backed up at the time, all the files were lost, so I had to take remedial measures, some data is recovered using the file recovery software, but all file names are lost. All files have only the code, as shown in the figure below:

Tens of thousands of files need to be manually changed to next year. Therefore, I wrote a python script to replace this complicated operation.

Implementation Analysis

It is impossible for the program to understand what is in my Word documents, but fortunately, all the content in my Word documents has titles, and most of the titles are exactly the file names of this document, as a result, I plan to treat the document title as a file name, and most of the file names are the first section of the content, so I have the idea. So I started to write programs.

Implementation Method

First, install the python-docx library and install it directly using pip:pip install python-docx

The specific procedure is as follows:

#-*-Coding: UTF-8-*-"NameChange1.0 This is a program that automatically modifies the name of an word document. author: fanghao "from docx import entimport OS # This is the directory where all the word files to be modified are stored. dir_1 =" C: \ Users \ visg \ Desktop \ 4 "filenames = OS. listdir (dir_1) # automatically modify for a in range (len (filenames): print (filenames [a]) dir_docx = dir_1 + "\" + filenames [a] try: document = Document (dir_docx) Does T: print ("error") else: new_name = document. paragraphs [0]. text + '.docx' try: OS. rename (dir_1 + OS. sep + filenames [a], dir_1 + OS. sep + new_name) T (FileNotFoundError, FileExistsError, OSError): print ("FileNotFoundError ")

You can modify the specific directory by yourself. Here, I skipped the duplicate files after the name is changed.

However, some of the recovered documents are docx, some are doc files, and docx files can be renamed through the above method, but the doc file has a problem. So we can only convert the doc file into a docx file first, and then use the above method to modify it. How to turn doc into docx, here there is a blog, a Daniel wrote a good plug-in can be a good implementation (http://blog.sina.com.cn/s/blog_5488e3a90100u8ux.html), the test is easy to use!

Note:The above method is acceptable for most Word documents, but some documents cannot be modified due to the format or other reasons. You can directly modify these documents manually.

Summary

The above is all the content of this article. I hope the content of this article has some reference and learning value for everyone's learning or work. If you have any questions, please leave a message to us, thank you for your support.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.