|
|
#1 |
|
Age improves with wine.
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 596
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? |
|
|
|
|
|
#2 |
|
Wizard
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 1,678
Karma: 9500498
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?
|
|
|
|
|
|
#3 |
|
Well trained by Cats
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 31,238
Karma: 61360164
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.
|
|
|
|
|
|
#4 |
|
Wizard
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 1,215
Karma: 1995558
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 07:17 PM. |
|
|
|
|
|
#5 |
|
Age improves with wine.
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 596
Karma: 95229
Join Date: Nov 2014
Device: Kindle Oasis, Kobo Libra II
|
|
|
|
|
|
|
#6 | |
|
Age improves with wine.
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 596
Karma: 95229
Join Date: Nov 2014
Device: Kindle Oasis, Kobo Libra II
|
Quote:
|
|
|
|
|
![]() |
|
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 11:59 AM |
| Kobo showing number of chapters/files | sebdea | Kobo Tablets | 1 | 03-17-2014 06:56 PM |
| Custom column to display number of chapters? | crackshot91 | Library Management | 10 | 09-08-2012 04:56 PM |
| Filenames to metadata, preserving filenames. | nitrogun | Calibre | 5 | 09-13-2010 10:50 PM |
| Change in number of chapters | James_Wilde | Calibre | 2 | 09-09-2010 11:24 AM |