Register Guidelines E-Books Today's Posts Search

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

Notices

Reply
 
Thread Tools Search this Thread
Old 10-18-2023, 06: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: 558
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, 01: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,103
Karma: 4911876
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, 01: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: 29,809
Karma: 54830978
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 online now   Reply With Quote
Old 10-18-2023, 07: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,091
Karma: 1948136
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 07:17 PM.
capink is offline   Reply With Quote
Old 10-22-2023, 08: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: 558
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, 08: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: 558
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


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 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


All times are GMT -4. The time now is 01:53 PM.


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