Register Guidelines E-Books Today's Posts Search

Go Back   MobileRead Forums > E-Book Formats > ePub

Notices

Reply
 
Thread Tools Search this Thread
Old 06-05-2014, 06: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: 2,608
Karma: 3000161
Join Date: Jan 2009
Device: Kindle PW3 (wifi)
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 06:50 AM.
roger64 is offline   Reply With Quote
Old 06-05-2014, 11:59 AM   #2
Doitsu
Grand Sorcerer
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: 5,584
Karma: 22735033
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
Advert
Old 06-05-2014, 12:20 PM   #3
BryanK
Connoisseur
BryanK can solve quadratic equations while standing on his or her head reciting poetry in iambic pentameterBryanK can solve quadratic equations while standing on his or her head reciting poetry in iambic pentameterBryanK can solve quadratic equations while standing on his or her head reciting poetry in iambic pentameterBryanK can solve quadratic equations while standing on his or her head reciting poetry in iambic pentameterBryanK can solve quadratic equations while standing on his or her head reciting poetry in iambic pentameterBryanK can solve quadratic equations while standing on his or her head reciting poetry in iambic pentameterBryanK can solve quadratic equations while standing on his or her head reciting poetry in iambic pentameterBryanK can solve quadratic equations while standing on his or her head reciting poetry in iambic pentameterBryanK can solve quadratic equations while standing on his or her head reciting poetry in iambic pentameterBryanK can solve quadratic equations while standing on his or her head reciting poetry in iambic pentameterBryanK can solve quadratic equations while standing on his or her head reciting poetry in iambic pentameter
 
Posts: 66
Karma: 12538
Join Date: Oct 2011
Device: Kobo Wifi, Kobo Glo HD
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 02:32 PM.
BryanK is offline   Reply With Quote
Old 06-05-2014, 03: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: 2,608
Karma: 3000161
Join Date: Jan 2009
Device: Kindle PW3 (wifi)
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 03:30 PM.
roger64 is offline   Reply With Quote
Old 06-05-2014, 03: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: 27,547
Karma: 193191846
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 03:37 PM.
DiapDealer is offline   Reply With Quote
Advert
Old 06-05-2014, 03: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: 2,608
Karma: 3000161
Join Date: Jan 2009
Device: Kindle PW3 (wifi)
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 03:44 PM. Reason: Calibre
roger64 is offline   Reply With Quote
Old 06-05-2014, 03: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: 27,547
Karma: 193191846
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, 05: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: 2,608
Karma: 3000161
Join Date: Jan 2009
Device: Kindle PW3 (wifi)
Quote:
Originally Posted by DiapDealer View Post
Let me experiment a bit.
Thanks for trying
roger64 is offline   Reply With Quote
Old 06-05-2014, 05:36 PM   #9
BryanK
Connoisseur
BryanK can solve quadratic equations while standing on his or her head reciting poetry in iambic pentameterBryanK can solve quadratic equations while standing on his or her head reciting poetry in iambic pentameterBryanK can solve quadratic equations while standing on his or her head reciting poetry in iambic pentameterBryanK can solve quadratic equations while standing on his or her head reciting poetry in iambic pentameterBryanK can solve quadratic equations while standing on his or her head reciting poetry in iambic pentameterBryanK can solve quadratic equations while standing on his or her head reciting poetry in iambic pentameterBryanK can solve quadratic equations while standing on his or her head reciting poetry in iambic pentameterBryanK can solve quadratic equations while standing on his or her head reciting poetry in iambic pentameterBryanK can solve quadratic equations while standing on his or her head reciting poetry in iambic pentameterBryanK can solve quadratic equations while standing on his or her head reciting poetry in iambic pentameterBryanK can solve quadratic equations while standing on his or her head reciting poetry in iambic pentameter
 
Posts: 66
Karma: 12538
Join Date: Oct 2011
Device: Kobo Wifi, Kobo Glo HD
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, 07: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: 27,547
Karma: 193191846
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, 234 views)
File Type: txt cm2em_v2.txt (2.0 KB, 207 views)

Last edited by DiapDealer; 06-05-2014 at 08:53 PM.
DiapDealer is offline   Reply With Quote
Old 06-06-2014, 01: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: 2,608
Karma: 3000161
Join Date: Jan 2009
Device: Kindle PW3 (wifi)
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 offline   Reply With Quote
Old 06-06-2014, 06: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: 27,547
Karma: 193191846
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, 09: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: 2,608
Karma: 3000161
Join Date: Jan 2009
Device: Kindle PW3 (wifi)
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 10:30 AM.
roger64 is offline   Reply With Quote
Old 06-06-2014, 09: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: 27,547
Karma: 193191846
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, 10: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: 2,608
Karma: 3000161
Join Date: Jan 2009
Device: Kindle PW3 (wifi)
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 offline   Reply With Quote
Reply


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Question: Find/Replace in css stylesheet ElMiko Sigil 12 12-25-2011 06:29 AM
ePub margins : @page vs stylesheet.css Agama Calibre 1 08-16-2010 04:37 PM
How to maintain a global CSS stylesheet amoroso Sigil 7 07-18-2010 08:37 PM
EPub conversion stylesheet.css problem nycaleksey Calibre 1 03-25-2010 11:42 PM
Where is the stylesheet.css? roger64 Sigil 4 03-23-2010 02:12 PM


All times are GMT -4. The time now is 05:33 AM.


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