![]() |
#1 |
Wizard
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 1,611
Karma: 9500498
Join Date: Sep 2021
Location: Australia
Device: Kobo Libra 2
|
Batch create xhtml pages
Hello,
When editing an ebook, I create a new xhtml file then paste html code for chapter headings, then fill in the chapter headings and then copy across the text. I do this for each chapter one at a time. Is there any way to batch create, say, 20 pages which includes my html code for the chapter headings? It would be even more helpful if the chapter numbers in the html code could be added also. (but that might be asking too much) |
![]() |
![]() |
![]() |
#2 |
Interested in the matter
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 421
Karma: 426094
Join Date: Dec 2011
Location: Spain, south coast
Device: Pocketbook InkPad 3
|
https://manual.calibre-ebook.com/edit.html#id34
You can also split a single HTML file at multiple locations automatically, by right clicking inside the file in the editor and choosing Split at multiple locations. This will allow you to easily split a large file at all heading tags or all tags having a certain class and so on. |
![]() |
![]() |
![]() |
#3 | |
Wizard
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 1,611
Karma: 9500498
Join Date: Sep 2021
Location: Australia
Device: Kobo Libra 2
|
Quote:
|
|
![]() |
![]() |
![]() |
#4 |
Groupie
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 167
Karma: 1497966
Join Date: Jul 2021
Device: N/A
|
You could create in the editor a page that will serve as a template.
Then you save it as an HTML file. Copy it 20 times (in your OS, with the file manager, not in the editor) Import those 20 files in your book (menu Files/Import files in the book). As they are HTML files, they will be insert in the "Text" part of the epub. Now you have just to modify chapter numbers and insert your text. You may be interested also by the snippets, a very powerful way to insert some text or tags in the current of the cursor, with Ctrl-J. : The interest over copying from clipboard is that it will be remembered in any of you editing session. ----- As for incrementing the chapter number, you can create a regex-function to do so across your book. Say you have all chapters in this form : <h1>Chapter 1</h1> Then, with the mode "Regex-function" active in you search box, you search for: (<h1>Chapter\s)(\d+)(</h1>) and you create a function "incr_chapter_number" : Code:
def replace(match, number, file_name, metadata, dictionaries, data, functions, *args, **kwargs): i = match.group(2) if i: return match.group(1) + str(int(i) +1) + match.group(3) return match.group(0) <h1>— 1 — This is the first chapter</h1> they will be matched with (<h1>[^\d]+)(\d+)([^<]+)(</h1>) and the return of the function should be : return match.group(1) + str(int(i) +1) + match.group(3) + match.group(4) Last edited by lomkiri; 09-30-2021 at 07:05 AM. |
![]() |
![]() |
![]() |
#5 |
Groupie
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 167
Karma: 1497966
Join Date: Jul 2021
Device: N/A
|
Ooops sorry, for the regex-function, you don't want to increment the existing chapter number, but to create a number from 1 to x, so the code would be (saying the form is "<h1>Chapter 1</h1>")
Code:
def replace(match, number, file_name, metadata, dictionaries, data, functions, *args, **kwargs): data.setdefault('chapnum', 0) if match.group(2): data['chapnum'] += 1 return match.group(1) + str(data['chapnum']) + match.group(3) return match.group(0) |
![]() |
![]() |
![]() |
#6 | ||||
Wizard
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 1,611
Karma: 9500498
Join Date: Sep 2021
Location: Australia
Device: Kobo Libra 2
|
Hello @lomkiri, Thank you for the detailed response.
Quote:
Quote:
Quote:
Quote:
PHP Code:
PHP Code:
I have modified the Search regex to: PHP Code:
Any chance you could show me what to change. Thanks for your efforts above ![]() Last edited by Karellen; 09-30-2021 at 11:26 PM. |
||||
![]() |
![]() |
![]() |
#7 |
Wizard
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 1,611
Karma: 9500498
Join Date: Sep 2021
Location: Australia
Device: Kobo Libra 2
|
Ok, I figured it out. My Search Regex was wrong.
I changed it to this... PHP Code:
PHP Code:
Do you know of any way to convert Number to Word eg 17 to Seventeen? |
![]() |
![]() |
![]() |
#8 | ||
Groupie
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 167
Karma: 1497966
Join Date: Jul 2021
Device: N/A
|
Quote:
Quote:
Saying your search string is (^\s*<[^>]+>)([^<\n]*)(<[^>]+>\n) to target a line with a number inside a tag, ex: <h2>seventeen</h2> or <p>twenty one</p> (adapt it better if necessary) the function will be : Code:
def words2int(textnum, numwords={}): # create our default word-lists if not numwords: # singles units = [ "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", ] # tens tens = ["", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"] # larger scales scales = ["hundred", "thousand", "million", "billion", "trillion"] # divisors numwords["and"] = (1, 0) # perform our loops and start the swap for idx, word in enumerate(units): numwords[word] = (1, idx) for idx, word in enumerate(tens): numwords[word] = (1, idx * 10) for idx, word in enumerate(scales): numwords[word] = (10 ** (idx * 3 or 2), 0) # primary loop current = result = 0 # loop while splitting to break into individual words for word in textnum.replace("-"," ").split(): # if problem then fail-safe if word not in numwords: raise Exception("Illegal word: " + word) # use the index by the multiplier scale, increment = numwords[word] current = current * scale + increment # if larger than 100 then push for a round 2 if scale > 100: result += current current = 0 # return the result plus the current return result + current def replace(match, number, file_name, metadata, dictionaries, data, functions, *args, **kwargs): wrd = match.group(2) if wrd: return match.group(1) + str(words2int(wrd)) + match.group(3) # or, same thing : return "{}{}{}".format(match.group(1), words2int(wrd), match.group(3)) return match.group(0) Last edited by lomkiri; 10-01-2021 at 06:01 AM. |
||
![]() |
![]() |
![]() |
#9 |
Groupie
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 167
Karma: 1497966
Join Date: Jul 2021
Device: N/A
|
NOTE : This function raises an exception if the targeted string is not a number, maybe you want to change this so in that case it returns the input without changes and doesn't raise an exception. If you keep it like this, just be sure that you capture only numbers in your regex.
|
![]() |
![]() |
![]() |
#10 | ||
Groupie
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 167
Karma: 1497966
Join Date: Jul 2021
Device: N/A
|
Quote:
(the OP has taken the same path indeed, the code of his function comes from the same stackoverflow's link) |
||
![]() |
![]() |
![]() |
#11 |
Wizard
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 1,611
Karma: 9500498
Join Date: Sep 2021
Location: Australia
Device: Kobo Libra 2
|
Hi @lomkiri
Sorry for the delay in responding. I was a bit busy on the weekend. Thank you for the above. (and silly me for not putting in more of an effort to spot that other thread) Looking at the above, it may take me a bit of time to comprehend the code and adjust it. My python skills = 0% So let me play with it over the coming week and see if I can figure it out. But part of me is thinking the effort may not be worth it as I can just as quickly type the words as needed. So this will be more of a "challenge" as I have the time available. ![]() ps (I tried to give you some karma points, but apparently I can't until I give others karma points first ) |
![]() |
![]() |
![]() |
#12 |
Bibliophagist
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 46,210
Karma: 168983734
Join Date: Jul 2010
Location: Vancouver
Device: Kobo Sage, Libra Colour, Lenovo M8 FHD, Paperwhite 4, Tolino epos
|
At one time, I created a dummy epub file which I use as a template. It has a cover, 15 chapter files and several copyright, dedication, etc. xhtml files. Basically, I just open it and save it as the new file name after the first basic edits.
|
![]() |
![]() |
![]() |
#13 | |
Wizard
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 1,611
Karma: 9500498
Join Date: Sep 2021
Location: Australia
Device: Kobo Libra 2
|
Quote:
This book has short, but many chapters - 118 chapters in this book which is why I was looking for shortcuts. The big help was the shortcut suggested by lomkiri and copying pages outside of calibre, then the regex-function to add the numbers. Then I run a search and replace to add prefixes such as "Fifty-", "Sixty-" etc. So I am about 95% of the way there and its not too painful. |
|
![]() |
![]() |
![]() |
#14 | ||
Groupie
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 167
Karma: 1497966
Join Date: Jul 2021
Device: N/A
|
Quote:
From the code you've shown us, your number will always be included in a paragraph of this form: <p class="chapterh2">Seventeen</p> so the regex part is pretty easy: (<p class="chapterh2">)([^<]+)(</p>) seems to be enough to do the job in auto. Quote:
https://python.swaroopch.com/ |
||
![]() |
![]() |
![]() |
#15 | |
Morlock
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 34
Karma: 2734796
Join Date: Oct 2021
Device: Kindle Paperwhite
|
Quote:
I had an e-book where the notes and the references were out of sync and I needed to add a digit or two to the id. Searching through the manual I found BuiltinAdd. But my skill set is too limited to allow me to be able to create a regex function that would make use of that. To my surprise, while browsing the forum to see if my limited skills could still help someone else, I came across your solution to @Karellen's problem. It worked perfectly for me. The e-book is legible (and what's more--clickable) again. Yahoo! |
|
![]() |
![]() |
![]() |
Thread Tools | Search this Thread |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Create new section.xhtml from Python | dxcore35 | Editor | 2 | 03-02-2020 07:47 AM |
Can I create a template for the xhtml files? | Banjo | Sigil | 9 | 01-02-2019 07:37 PM |
mkepub tool to create epub from xhtml | sethportman | ePub | 0 | 02-03-2012 08:59 AM |
PRS-T1 How to create a highlight across 2 pages? | diddy | Sony Reader | 0 | 01-15-2012 02:16 PM |
Python script: batch create mobi's from epubs (if they don't exist) | jmeb | Conversion | 0 | 05-20-2011 01:14 PM |