Batchload rename .docx file to the title of document. (+ tip)
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

Need to change .docx files names to the first title on the text.

file.docx --> change to whatevertitleisondocumentis.docx more than 100 at time.

has to be for linux/ubuntu.

Thanx everyone

Are titles always the first sentence in the document?
feroldi 3 months ago
And could you also upload a docx document for demonstration?
feroldi 3 months ago
Yes, its always the first sentence.
Tsi 3 months ago
how to upload doc through Bountify
Tsi 3 months ago
What should happen if two or more documents have the same title?
feroldi 3 months ago
this wont happen all docs have different titles, in all cases a example, exampe1 should be added for text with same titles.
Tsi 3 months ago
awarded to feroldi

Crowdsource coding tasks.

1 Solution

My solution is written in Python 3.7. It depends on python-docx. Simply install it with pip (use the correct version of pip, i.e. pip3.7):

pip3 install --user python-docx

After installing its dependency, you're able to run the script. Here's and example of a run:

python3 /path/to/docs 

The program accepts both a word document file, or a directory that contains word document files. In the former, it just renames that file. In the latter, it renames all files in that directory, adding a suffix -N after the title in case of duplicates, where N starts from 0 onwards.

Passing the parameter -v makes it display more information about the process.

In case you're working with .doc files (the 2003 version), then you have to convert them to .docx using soffice:

cd /path/to/docs
soffice --convert-to docx *.doc
rm *.doc

Here's the program:

#!/usr/bin/env python3

import argparse
import sys
import os
import pathlib
import docx
import logging
import subprocess
import errno

logger = logging.getLogger(__name__)
        '[%(asctime)s] {%(filename)s:%(lineno)d} %(levelname)s - %(message)s'))

def find_doc_title(doc):
    for paragrah in doc.paragraphs:
        if paragrah.text:
            return paragrah.text
    return None

def rename_document_to_title(doc_path, titles):
    doc = docx.Document(doc_path)
    title = find_doc_title(doc)
    if title:
        if title in titles:
            suffix = f'-{titles[title]}'
            titles[title] += 1
            suffix = ''
            titles[title] = 0
        title_words = title.split()
        while True:
            title = ' '.join(title_words)
            titled_doc_name = f'{title}{suffix}.docx'
                os.rename(doc_path, doc_path.parent / titled_doc_name)
            except OSError as e:
                if e.errno == errno.ENAMETOOLONG:
        logger.debug(f'renamed `{doc_path}` to `{titled_doc_name}`')
        logger.debug(f'skipping `{doc_path}`: no title found')

if __name__ == '__main__':
    args = argparse.ArgumentParser()
        help='path to a docx file, or a directory containing docx files')
        help='display more information')
    opts = args.parse_args()
    logger.setLevel(logging.DEBUG if opts.verbose else logging.INFO)
    documents_path = pathlib.Path(opts.path)
    titles = {}
    if documents_path.is_dir():
        for docx_path in documents_path.glob('*.docx'):
            rename_document_to_title(docx_path, titles)
        rename_document_to_title(documents_path, titles)
i notice this happens when name is too long, is there a way to modify this, titiles are toooo long.
Tsi 3 months ago
I just tested it with long titles, and it works fine. Perhaps you could send me the problematic docx via e-mail (you can find it on my profile) and I can take a look at it.
feroldi 3 months ago
27 words long title?... cause i cant change the title manually even if i want i got a message in ubuntu saying is too long.
Tsi 3 months ago
In that case, one solution is to limit the title to N words, or to take only the initials of each word only when the title is too long, or to just ignore that file and do nothing with it. What do you prefer?
feroldi 3 months ago
limit to N words as there are many files longer than 25 words.
Tsi 3 months ago
The new script is now updated on my post.
feroldi 3 months ago
didnt find the email on the profile
Tsi 3 months ago
Ace!... now its working perfectly!!! How many files can be renamed at once?
Tsi 3 months ago
Great. :)
feroldi 3 months ago
how can i contactyou?
Tsi 3 months ago
My e-mail is on my github page:
feroldi 3 months ago
last thing is / on subtitle is not working
Tsi 3 months ago