View Single Post
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 online now   Reply With Quote