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

Go Back   MobileRead Forums > E-Book Formats > ePub

Notices

Reply
 
Thread Tools Search this Thread
Old 06-05-2014, 07:41 AM   #1
roger64
Wizard
roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.
 
Posts: 1,496
Karma: 846401
Join Date: Jan 2009
Device: KoboGlo
Converting cm to em in css stylesheet

Hi

I have a Python script that has been written on another forum. It works well: it allows to modify any stylesheet.css file containing units in cm (centimeters) to units in em. It limits the conversion result to two decimals (so as to avoid likes of 1.254678952em).

To use it, I have to export the stylesheet from the EPUB, modify it and import it again: this is not very convenient for such a small matter.

I would like to complete this script so as to enable it to modify directly the stylesheet of an EPUB.

Can someone help for this? Here is the script:

Spoiler:
#!/usr/bin/python

import sys, re

conv=2.37106301584
unit=re.compile('([^\d.]+:\s*)([\d.]+)(cm)?(;\s*)')
with open(sys.argv[1],'r') as f, open(sys.argv[2],'w') as g :
for lig in f :
k=unit.search(lig)
if k :
unite=k.group(3)
if unite=='cm' :
val="%.2f"%(conv*float(k.group(2)))
unite='em'
lig=k.group(1)+val+unite+k.group(4)
g.write(lig)


It can be launched this way: ./your-script-name old-stylesheet.css new-stylesheet.css


PS: I am aware about rem for EPUB3 and potential troubles for nested ems. This is for use for a plain EPUB2, without ordered lists.

Last edited by roger64; 06-05-2014 at 07:50 AM.
roger64 is online now   Reply With Quote
Old 06-05-2014, 12:59 PM   #2
Doitsu
Wizard
Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.
 
Doitsu's Avatar
 
Posts: 2,132
Karma: 4938208
Join Date: Dec 2010
Device: Kindle PW2
Sigil supports the execution of python scripts via batch files or shell scripts. For more information see this thread. However, you'd probably have to modify the Python script for it. (I haven't figured out the details of this feature, but DiapDealer, who discovered it, can probably help you with this.)

If the app that generates styles with cm values instead of em values creates only a limited number of predictable cm values, you could also create a Saved Search group in Sigil and have it replace these values automatically via a group search.
Doitsu is offline   Reply With Quote
 
Advertisement
Old 06-05-2014, 01:20 PM   #3
BryanK
Enthusiast
BryanK is a glorious beacon of lightBryanK is a glorious beacon of lightBryanK is a glorious beacon of lightBryanK is a glorious beacon of lightBryanK is a glorious beacon of lightBryanK is a glorious beacon of lightBryanK is a glorious beacon of lightBryanK is a glorious beacon of lightBryanK is a glorious beacon of lightBryanK is a glorious beacon of lightBryanK is a glorious beacon of light
 
Posts: 29
Karma: 12062
Join Date: Oct 2011
Device: Kobo Wifi
Python has a 'subprocess' module that allows you to make system calls just like you were typing them on the command line. If you can post the commands you're using to unzip/zip your epub it should be straightforward to modify the script.

Edit:
If you could also re-post the script using the [CODE] wrapper rather than [SPOILER], Python is really particular about spacing and [SPOILER] doesn't preserve it. Hopefully, posting 15 or so lines of code won't give anybody heartburn.

Last edited by BryanK; 06-05-2014 at 03:32 PM.
BryanK is offline   Reply With Quote
Old 06-05-2014, 04:12 PM   #4
roger64
Wizard
roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.
 
Posts: 1,496
Karma: 846401
Join Date: Jan 2009
Device: KoboGlo
Here is it. I do not have anything more than these lines. I wish I had.

Code:
#!/usr/bin/python

import sys, re

conv=2.37106301584
unit=re.compile('([^\d.]+:\s*)([\d.]+)(cm)?(;\s*)')
with open(sys.argv[1],'r') as f, open(sys.argv[2],'w') as g :
   for lig in f :
      k=unit.search(lig)
      if k :
         unite=k.group(3)
         if unite=='cm' :
            val="%.2f"%(conv*float(k.group(2)))
            unite='em'
            lig=k.group(1)+val+unite+k.group(4)
      g.write(lig)
I also had a script that had been given to me by Jellby to insert directly a file in META-INF without opening the EPUB. I thought that a similar process could be used here.

Code:
for i in *.epub; do zip "$i" META-INF/com.apple.ibooks.display-options.xml; done

Last edited by roger64; 06-05-2014 at 04:30 PM.
roger64 is online now   Reply With Quote
Old 06-05-2014, 04:25 PM   #5
DiapDealer
Grand Sorcerer
DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.
 
DiapDealer's Avatar
 
Posts: 9,540
Karma: 44104176
Join Date: Jan 2010
Device: Nexus 7, Kindle Fire HD
Modifying a file in an ePub archive with python is certainly possible, but it can be tricky and more than a bit convoluted. Python's standard ZipFile module can read an archive's contents; it can add files to archives; and it can create new archives. What it can't do is modify a file in an archive directly, or delete a file from an archive, or overwrite it.

You'd basically have to extract the contents of the ePub archive (maybe to a temp directory) first, then modify (overwrite/copy/rename/whatever) the css file(s) and then create a new zipfile from the modified, extracted content of the original (making sure to add the mimetype file first and uncompressed so it's a valid epub).

Last edited by DiapDealer; 06-05-2014 at 04:37 PM.
DiapDealer is offline   Reply With Quote
Old 06-05-2014, 04:33 PM   #6
roger64
Wizard
roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.
 
Posts: 1,496
Karma: 846401
Join Date: Jan 2009
Device: KoboGlo
Quote:
Originally Posted by DiapDealer View Post
Modifying a file in an ePub archive with python is certainly possible, but it can be tricky and more than a bit convoluted. Python's standard ZipFile module can read an archive contents; it can add files to archives; and it can create new archives. What it can't do is modify a file in an archive directly, or delete a file from an archive, or overwrite it.

You'd basically have to extract the contents of the ePub archive (maybe to a temp directory) first, then modify (overwrite/copy/rename/whatever) the css file(s) and then create a new zipfile from the modified, extracted content of the original (making sure to add the mimetype file first and uncompressed so it's a valid epub).
With the Calibre editor, I can export a stylesheet, then with the script I modify it and I import the modified file to replace the original stylesheet. Long to explain but technically very easy.

I was hoping I could somewhat modify directly the stylesheet. If this is not possible with a plain solution, I will propose the script to Kovid Goyal to include it within the many functions of his nice editor.

Last edited by roger64; 06-05-2014 at 04:44 PM. Reason: Calibre
roger64 is online now   Reply With Quote
Old 06-05-2014, 04:50 PM   #7
DiapDealer
Grand Sorcerer
DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.
 
DiapDealer's Avatar
 
Posts: 9,540
Karma: 44104176
Join Date: Jan 2010
Device: Nexus 7, Kindle Fire HD
Quote:
Originally Posted by roger64 View Post
With the Calibre editor, I can export a stylesheet, then with the script I modify it and I import the modified file to replace the original stylesheet. Long to explain but technically very easy.
Yes. Easy because calibre's editor handles the extraction/rebuilding of the ePub (zip) behind the scenes for you.

Quote:
Originally Posted by roger64 View Post
I was hoping I could somewhat modify directly the stylesheet.
It could still be done ... but the resulting script would need to take on the extraction/rebuilding process that calibre's editor already does for you.

There might be a few shortcuts to take. Let me experiment a bit.
DiapDealer is offline   Reply With Quote
Old 06-05-2014, 06:24 PM   #8
roger64
Wizard
roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.
 
Posts: 1,496
Karma: 846401
Join Date: Jan 2009
Device: KoboGlo
Quote:
Originally Posted by DiapDealer View Post
Let me experiment a bit.
Thanks for trying
roger64 is online now   Reply With Quote
Old 06-05-2014, 06:36 PM   #9
BryanK
Enthusiast
BryanK is a glorious beacon of lightBryanK is a glorious beacon of lightBryanK is a glorious beacon of lightBryanK is a glorious beacon of lightBryanK is a glorious beacon of lightBryanK is a glorious beacon of lightBryanK is a glorious beacon of lightBryanK is a glorious beacon of lightBryanK is a glorious beacon of lightBryanK is a glorious beacon of lightBryanK is a glorious beacon of light
 
Posts: 29
Karma: 12062
Join Date: Oct 2011
Device: Kobo Wifi
Quote:
Originally Posted by DiapDealer View Post
Python's standard ZipFile module can read an archive's contents; it can add files to archives; and it can create new archives. What it can't do is modify a file in an archive directly, or delete a file from an archive, or overwrite it.
Which is one reason I was planning on using 'subprocess.call' to call something like 'zip mybook.epub OEBPS/stylesheet.css'.
BryanK is offline   Reply With Quote
Old 06-05-2014, 08:01 PM   #10
DiapDealer
Grand Sorcerer
DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.
 
DiapDealer's Avatar
 
Posts: 9,540
Karma: 44104176
Join Date: Jan 2010
Device: Nexus 7, Kindle Fire HD
No promises, but this seems to be working for me. I stayed strictly within Python ... so it's going to leave the original epub unchanged and create a new one with the desired changes.

Just like your original script, it's launched as follows:
Code:
./cm2em.py old.epub new.epub
There's not much in the way of error-checking, so that might need some work. And the script doesn't "fix" anything. If it wasn't a valid epub archive before, it's not going to be valid after. But by the same token ... if the original was a validated epub to begin with, the script shouldn't break it (structurally speaking, anyway). It just runs your conversion routine on any css files found in any zip archive (epub) you feed to it.

The file extension is .txt so I could upload it easily. Change it to .py if it amuses you.

EDIT: Got rid of an unnecessary conditional and changed it so if the stylesheet didn't need altering, it isn't unnecessarily rewritten. That way, all file properties (modification date, etc...) should stay the same (unless they were altered). Can't guarantee that Python's zlib compression implementation will result in a compressed size that exactly matches whatever was used to create the epub originally, but it should be close.
Attached Files
File Type: txt cm2em.txt (2.0 KB, 18 views)
File Type: txt cm2em_v2.txt (2.0 KB, 22 views)

Last edited by DiapDealer; 06-05-2014 at 09:53 PM.
DiapDealer is offline   Reply With Quote
Old 06-06-2014, 02:08 AM   #11
roger64
Wizard
roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.
 
Posts: 1,496
Karma: 846401
Join Date: Jan 2009
Device: KoboGlo
Hi

I tried to launch the v2 the way I launched the original script but I failed ("No file or folder of this kind"). Maybe I have some library missing?

Code:
roger@lmde64:~/Bureau/trial$ ./cm2em_v2.py traite.epub traite2.epub
: Aucun fichier ou dossier de ce type
roger@lmde64:~/Bureau/trial$
roger64 is online now   Reply With Quote
Old 06-06-2014, 07:03 AM   #12
DiapDealer
Grand Sorcerer
DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.
 
DiapDealer's Avatar
 
Posts: 9,540
Karma: 44104176
Join Date: Jan 2010
Device: Nexus 7, Kindle Fire HD
Make the script executable (chmod a+x cm2em_v2.py) or specify the interpreter on the command-line:
python cm2em_v2.py traite.epub traite2.epub.
DiapDealer is offline   Reply With Quote
Old 06-06-2014, 10:44 AM   #13
roger64
Wizard
roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.
 
Posts: 1,496
Karma: 846401
Join Date: Jan 2009
Device: KoboGlo
Hi

It's working very well. Thanks for your help. Of course, I had made the script executable but I needed the "python" before... Now, it's very handy.

Again, thanks for your help.

PS: credits to pingouinux (French Ubuntu Forum) and DiapDealer.

Last edited by roger64; 06-06-2014 at 11:30 AM.
roger64 is online now   Reply With Quote
Old 06-06-2014, 10:57 AM   #14
DiapDealer
Grand Sorcerer
DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.
 
DiapDealer's Avatar
 
Posts: 9,540
Karma: 44104176
Join Date: Jan 2010
Device: Nexus 7, Kindle Fire HD
Glad it’s working for you. You can probably change the initial shabang line to match what you had in your original script if you want the convenience of launching it without typing "python".
DiapDealer is offline   Reply With Quote
Old 06-06-2014, 11:29 AM   #15
roger64
Wizard
roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.roger64 ought to be getting tired of karma fortunes by now.
 
Posts: 1,496
Karma: 846401
Join Date: Jan 2009
Device: KoboGlo
Your first line is OK because I use this
Code:
python cm2em.py traite.epub traite2.epub
and I don't need anymore the unintuitive
Code:
 ./
before the name of the script.
roger64 is online now   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
Question: Find/Replace in css stylesheet ElMiko Sigil 12 12-25-2011 07:29 AM
ePub margins : @page vs stylesheet.css Agama Calibre 1 08-16-2010 05:37 PM
How to maintain a global CSS stylesheet amoroso Sigil 7 07-18-2010 09:37 PM
EPub conversion stylesheet.css problem nycaleksey Calibre 1 03-26-2010 12:42 AM
Where is the stylesheet.css? roger64 Sigil 4 03-23-2010 03:12 PM


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


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