Python bulk modifies Word file name

Source: Internet
Author: User
Tags python script

Recently accidentally put the hard disk to format, because the file is not backed up, so all the files are not, so only to take remedial measures, recover a part of the data with the file recovery software, but the file name of the recovered files are all lost, all the files are only code, such as the following figure:

Tens of thousands of files This is a manual change to next year. So I wrote a python script to replace this cumbersome operation.

main idea: it is impossible for the program to understand what is in my Word document, but fortunately my Word document content has a title, most of the title is exactly the file name of this document, so I intend to take the title of the document as the file name, And most of the file name is the first paragraph of the content, so the idea has. Then began to write the program to open dry.

First you need to install the Python-docx library and install it directly via PIP: Pip install Python-docx

The specific procedures are as follows:

#-*-Coding:utf-8-*-"" "Namechange1.0this is a program that automatically modifies the name of a Word document.  Author:fanghao "" "from docx import Documentimport os# This is the directory where all the word files to be modified dir_1 =" C:\\users\\visg\\desktop\\4 " Filenames = Os.listdir (dir_1) #自动修改for A in range (Len (filenames)):    print (Filenames[a])    dir_docx = dir_1 + "\ \" + Filenames[a]    Try:        document = document (Dir_docx)    except:        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) C11/>except (filenotfounderror,fileexistserror,oserror):            print ("Filenotfounderror")

The specific directory of their own changes can be, here I skip directly over what name changed after the duplicate file.

But there is a bit of a hole in the recovery of the document is a few docx, there are doc documents, docx documents can be renamed by the above method, but the doc document has a problem. Therefore, the DOC document can only be converted to a docx document, and then modified using the method described above. How to turn Doc into docx, here is a blog, a Daniel wrote a more positive plug-in can be very good implementation (http://blog.sina.com.cn/s/blog_5488e3a90100u8ux.html), pro-Test easy to use!

Note: The above methods are available for most Word documents, but some documents cannot be modified for their own reasons, such as formatting, and can be modified directly for these documents.

Python bulk modifies Word file name

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.