10-18-2023, 07:26 AM | #1 |
Age improves with wine.
Posts: 571
Karma: 95229
Join Date: Nov 2014
Device: Kindle Oasis, Kobo Libra II
|
Using filenames to number chapters?
I have a bunch of files called chapter1.html, chapter2.html, ... where the chapters themselves have not been numbered.
I want to take the file name and insert it into the text of the file. Is there an easy way to do this? |
10-18-2023, 02:38 PM | #2 |
Wizard
Posts: 1,356
Karma: 6794938
Join Date: Sep 2021
Location: Australia
Device: Kobo Libra 2
|
Is the reason for that because you don't want to manually add the chapter numbers as there may be a lot of chapters?
|
10-18-2023, 02:58 PM | #3 |
Well trained by Cats
Posts: 30,444
Karma: 58055868
Join Date: Aug 2009
Location: The Central Coast of California
Device: Kobo Libra2,Kobo Aura2v1, K4NT(Fixed: New Bat.), Galaxy Tab A
|
Making a code Snippet , would speed things up. You just add the missing piece.
|
10-18-2023, 08:12 PM | #4 |
Wizard
Posts: 1,139
Karma: 1954142
Join Date: Aug 2015
Device: Kindle
|
Depending on where you want to insert the text, you can utilize the replace function mode with the function below
Code:
import regex def replace(match, number, file_name, metadata, dictionaries, data, functions, *args, **kwargs): if not file_name.lower().startswith('chapter'): return match.group() name = regex.sub(r'(.+?)(\d+)\.x?html?', r'\1 \2', file_name).title() header = f'<h1>{name}</h1>' return regex.sub('(<body[^>]*>)', r'\1' + '\n' + header, match.group(), regex.MULTILINE) Then press replace all. This will insert the chapter heading as a h1 header at the very top (just below <body>) Last edited by capink; 10-18-2023 at 08:17 PM. |
10-22-2023, 09:34 AM | #5 |
Age improves with wine.
Posts: 571
Karma: 95229
Join Date: Nov 2014
Device: Kindle Oasis, Kobo Libra II
|
|
10-22-2023, 09:35 AM | #6 | |
Age improves with wine.
Posts: 571
Karma: 95229
Join Date: Nov 2014
Device: Kindle Oasis, Kobo Libra II
|
Quote:
|
|
Thread Tools | Search this Thread |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Any source for the number of chapters in a book? | ownedbycats | Reading Recommendations | 10 | 02-14-2023 12:59 PM |
Kobo showing number of chapters/files | sebdea | Kobo Tablets | 1 | 03-17-2014 07:56 PM |
Custom column to display number of chapters? | crackshot91 | Library Management | 10 | 09-08-2012 05:56 PM |
Filenames to metadata, preserving filenames. | nitrogun | Calibre | 5 | 09-13-2010 11:50 PM |
Change in number of chapters | James_Wilde | Calibre | 2 | 09-09-2010 12:24 PM |