Register Guidelines E-Books Search Today's Posts Mark Forums Read

Go Back   MobileRead Forums > E-Book Software > Calibre > Editor

Notices

Reply
 
Thread Tools Search this Thread
Old 10-18-2023, 07:26 AM   #1
Phssthpok
Age improves with wine.
Phssthpok knows how to set a laser printer to stun.Phssthpok knows how to set a laser printer to stun.Phssthpok knows how to set a laser printer to stun.Phssthpok knows how to set a laser printer to stun.Phssthpok knows how to set a laser printer to stun.Phssthpok knows how to set a laser printer to stun.Phssthpok knows how to set a laser printer to stun.Phssthpok knows how to set a laser printer to stun.Phssthpok knows how to set a laser printer to stun.Phssthpok knows how to set a laser printer to stun.Phssthpok knows how to set a laser printer to stun.
 
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?
Phssthpok is offline   Reply With Quote
Old 10-18-2023, 02:38 PM   #2
Karellen
Wizard
Karellen ought to be getting tired of karma fortunes by now.Karellen ought to be getting tired of karma fortunes by now.Karellen ought to be getting tired of karma fortunes by now.Karellen ought to be getting tired of karma fortunes by now.Karellen ought to be getting tired of karma fortunes by now.Karellen ought to be getting tired of karma fortunes by now.Karellen ought to be getting tired of karma fortunes by now.Karellen ought to be getting tired of karma fortunes by now.Karellen ought to be getting tired of karma fortunes by now.Karellen ought to be getting tired of karma fortunes by now.Karellen ought to be getting tired of karma fortunes by now.
 
Karellen's Avatar
 
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?
Karellen is offline   Reply With Quote
Old 10-18-2023, 02:58 PM   #3
theducks
Well trained by Cats
theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.
 
theducks's Avatar
 
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.
theducks is offline   Reply With Quote
Old 10-18-2023, 08:12 PM   #4
capink
Wizard
capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.
 
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)
The search string should be: .+
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.
capink is offline   Reply With Quote
Old 10-22-2023, 09:34 AM   #5
Phssthpok
Age improves with wine.
Phssthpok knows how to set a laser printer to stun.Phssthpok knows how to set a laser printer to stun.Phssthpok knows how to set a laser printer to stun.Phssthpok knows how to set a laser printer to stun.Phssthpok knows how to set a laser printer to stun.Phssthpok knows how to set a laser printer to stun.Phssthpok knows how to set a laser printer to stun.Phssthpok knows how to set a laser printer to stun.Phssthpok knows how to set a laser printer to stun.Phssthpok knows how to set a laser printer to stun.Phssthpok knows how to set a laser printer to stun.
 
Posts: 571
Karma: 95229
Join Date: Nov 2014
Device: Kindle Oasis, Kobo Libra II
Quote:
Originally Posted by Karellen View Post
Is the reason for that because you don't want to manually add the chapter numbers as there may be a lot of chapters?
Yes, exactly.
Phssthpok is offline   Reply With Quote
Old 10-22-2023, 09:35 AM   #6
Phssthpok
Age improves with wine.
Phssthpok knows how to set a laser printer to stun.Phssthpok knows how to set a laser printer to stun.Phssthpok knows how to set a laser printer to stun.Phssthpok knows how to set a laser printer to stun.Phssthpok knows how to set a laser printer to stun.Phssthpok knows how to set a laser printer to stun.Phssthpok knows how to set a laser printer to stun.Phssthpok knows how to set a laser printer to stun.Phssthpok knows how to set a laser printer to stun.Phssthpok knows how to set a laser printer to stun.Phssthpok knows how to set a laser printer to stun.
 
Posts: 571
Karma: 95229
Join Date: Nov 2014
Device: Kindle Oasis, Kobo Libra II
Quote:
Originally Posted by capink View Post
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)
The search string should be: .+
Then press replace all.

This will insert the chapter heading as a h1 header at the very top (just below <body>)
Ah, excellent, many thanks!
Phssthpok is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Forum Jump

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


All times are GMT -4. The time now is 05:11 PM.


MobileRead.com is a privately owned, operated and funded community.