![]() |
#1 |
Wizard
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 2,623
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:
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. |
![]() |
![]() |
![]() |
#2 |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 5,680
Karma: 23983815
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. |
![]() |
![]() |
Advert | |
|
![]() |
#3 |
Connoisseur
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() 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. |
![]() |
![]() |
![]() |
#4 |
Wizard
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 2,623
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) 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. |
![]() |
![]() |
![]() |
#5 |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 28,353
Karma: 203720150
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. |
![]() |
![]() |
Advert | |
|
![]() |
#6 | |
Wizard
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 2,623
Karma: 3000161
Join Date: Jan 2009
Device: Kindle PW3 (wifi)
|
Quote:
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 |
|
![]() |
![]() |
![]() |
#7 | |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 28,353
Karma: 203720150
Join Date: Jan 2010
Device: Nexus 7, Kindle Fire HD
|
Quote:
![]() 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. |
|
![]() |
![]() |
![]() |
#8 |
Wizard
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 2,623
Karma: 3000161
Join Date: Jan 2009
Device: Kindle PW3 (wifi)
|
|
![]() |
![]() |
![]() |
#9 |
Connoisseur
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 66
Karma: 12538
Join Date: Oct 2011
Device: Kobo Wifi, Kobo Glo HD
|
Which is one reason I was planning on using 'subprocess.call' to call something like 'zip mybook.epub OEBPS/stylesheet.css'.
|
![]() |
![]() |
![]() |
#10 |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 28,353
Karma: 203720150
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 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. Last edited by DiapDealer; 06-05-2014 at 08:53 PM. |
![]() |
![]() |
![]() |
#11 |
Wizard
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 2,623
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$ |
![]() |
![]() |
![]() |
#12 |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 28,353
Karma: 203720150
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. |
![]() |
![]() |
![]() |
#13 |
Wizard
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 2,623
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. |
![]() |
![]() |
![]() |
#14 |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 28,353
Karma: 203720150
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".
![]() |
![]() |
![]() |
![]() |
#15 |
Wizard
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 2,623
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 Code:
./ |
![]() |
![]() |
![]() |
|
![]() |
||||
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 |