How to remove module prefix (parent) names from sphinx-apidoc generated titles?

Skip to first unread message

Shawn Hymel

Aug 17, 2023, 3:02:53 PM8/17/23
to sphinx-users
The closest I could find was to go through each .rst file and remove the package/module parents from the title. For example, in the file edgeimpulse_api.api.admin_api.rst, I changed:

edgeimpulse\_api.api.auth\_api module


auth\_api module

That helped make some of the longer titles much easier to read on the left sidebar and on the page itself (see screenshot, using Furo theme).

Is there any way to change the title like this automatically (either in sphinx-apidoc or sphinx-build) without needing to manually edit every .rst file? My search through docs and forums has not turned up anything. My backup plan is to create a pre-processing script that runs between sphinx-apidoc and sphinx-build to modify the titles in all .rst files (not pretty, so I'm hoping to avoid this option).
Screenshot 2023-08-17 at 11.15.35 AM.png

Shawn Hymel

Aug 18, 2023, 1:45:14 PM8/18/23
to sphinx-users
Here's my quick and dirty script that I added to to shorten all titles in the .rst source files. If anyone knows of a better/cleaner way to do this, please let me know!

# Recursively crawl through source directory and shorten titles in .rst files
def crawl_source_shorten_titles(path):

# List files in directory
for file_name in os.listdir(path):

# Build path to file
file_path = os.path.join(path, file_name)

# Recursively crawl to next directory level
if os.path.isdir(file_path):

# Modify .rst source file title
_, extension = os.path.splitext(file_path)
if extension == ".rst":

# Read file, modify title, write back to file
with open(file_path, 'r') as file:
lines = file.readlines()
lines[0] = lines[0].split('.')[-1]
lines[1] = ("=" * (len(lines[0]) - 1)) + "\n"
with open(file_path, 'w') as file:

# Remove parents from titles in all .rst files
if not show_title_parents:
Reply all
Reply to author
0 new messages