Scribus xhtml using Scripter: Difference between revisions

From Scribus Wiki
Jump to navigation Jump to search
 
(9 intermediate revisions by 2 users not shown)
Line 1: Line 1:
[[Category:Scripts]]
{{Scripting Index}}
Why not just do this straight up to xhtml with Scripter? Well, why not?
Why not just do this straight up to xhtml with Scripter? Well, why not?


This might serve as a quick way to get the content to a format easily importable to an ePub editor like Sigil.
This might serve as a quick way to get the content to a format easily importable to an ePub editor like Sigil.
Don't forget the siblings: [[Scribus XML using Scripter]] and [[Scribus files as XML]].


===export2xhtml.py===
===export2xhtml.py===
<pre>
<syntaxhighlight lang='python'>
#!/usr/bin/env python
#!/usr/bin/env python
# File: export2xhtml.py - Extracts the content from a document, saving to an xhtml file
# File: export2xhtml.py - Extracts the content from a document, saving to an xhtml file
# 2013.10.29 Gregory Pittman
# 2013.11.01 Gregory Pittman
# This program is free software; you can redistribute it and/or modify
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# it under the terms of the GNU General Public License as published by
Line 23: Line 27:
     T.append('<?xml version="1.0" encoding="UTF-8"?>\n')
     T.append('<?xml version="1.0" encoding="UTF-8"?>\n')
     T.append('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">\n')
     T.append('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">\n')
     T.append('<html xmlns="http://www.w3.org/1999/xhtml">\n<body>')
     T.append('<html xmlns="http://www.w3.org/1999/xhtml">\n<body>\n')
     T.append('<h2>Scribus ' + str(scribus.scribus_version_info[0]) + '.' + str(scribus.scribus_version_info[1]) + '.' + str(scribus.scribus_version_info[2]) + str(scribus.scribus_version_info[3]) + '</h2>\n')
     T.append('<h2>Scribus ' + str(scribus.scribus_version_info[0]) + '.' + str(scribus.scribus_version_info[1]) + '.' + str(scribus.scribus_version_info[2]) + str(scribus.scribus_version_info[3]) + '</h2>\n')
     while (page <= pagenum):
     while (page <= pagenum):
Line 46: Line 50:
                             char = '&amp;'
                             char = '&amp;'
                         elif ((ord(char) == 13) or (ord(char) == 10)):
                         elif ((ord(char) == 13) or (ord(char) == 10)):
                             char = "</p><p>"
                             char = "</p>\n<p>"         # the added newline makes the source look better
                         filtered = filtered + char
                         filtered = filtered + char
                     contents = filtered
                     contents = filtered
                 T.append('<table border="0" cellspacing="0"><tr><td width="700px"><p>' + contents + '</p>\n')
                 T.append('<table border="0" cellspacing="0"><tr><td width="700px"><p>' + contents + '</p>\n')
                 T.append('</p></td></tr></table>\n')
                 T.append('</p></td>\n</tr>\n</table>\n')
                 content.append(contents)
                 content.append(contents)
             elif (item[1] == 2):
             elif (item[1] == 2):
Line 56: Line 60:
                 imgsize = scribus.getSize(item[0])
                 imgsize = scribus.getSize(item[0])
                 picwidth = int(700 * imgsize[0]/pagesize[0])
                 picwidth = int(700 * imgsize[0]/pagesize[0])
                 T.append('<p><table border="0"><tr><td><img src="' + imgname + '" width="' + str(picwidth) + 'px" /></td></tr></table></p>\n')
                 T.append('<p><table border="0">\n<tr>\n<td><img src="' + imgname + '" width="' + str(picwidth) + 'px" /></td>\n</tr>\n</table></p>\n')
                  
                  
         page += 1
         page += 1
     T.append('</body></html>\n')
     T.append('</body>\n</html>\n')
     output_file = open(textfile,'w')
     output_file = open(textfile,'w')
     output_file.writelines(T)
     output_file.writelines(T)
     output_file.close()
     output_file.close()
     endmessage = textfile + ' was created'
     endmessage = textfile + ' was created'
     scribus.messageBox("Finished", endmessage,icon=0,button1=1)
     scribus.messageBox("Finished", endmessage,icon=scribus.ICON_NONE,button1=scribus.BUTTON_OK) # Formerly, you could use icon =1 and button1 = 1




Line 78: Line 82:


else:
else:
     scribus.messageBox('Export Error', 'You need a Document open, and a frame selected.', \
     scribus.messageBox('Export Error', 'You need a Document open.', \
                       icon=0, button1=1)
                       icon=scribus.ICON_NONE, button1=scribus.BUTTON_OK)


</pre>
</syntaxhighlight>

Latest revision as of 18:22, 3 October 2016

This article is part of the Scripts series.

Why not just do this straight up to xhtml with Scripter? Well, why not?

This might serve as a quick way to get the content to a format easily importable to an ePub editor like Sigil.

Don't forget the siblings: Scribus XML using Scripter and Scribus files as XML.

export2xhtml.py

#!/usr/bin/env python
# File: export2xhtml.py - Extracts the content from a document, saving to an xhtml file
# 2013.11.01 Gregory Pittman
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

import scribus


def exportText(textfile):
    page = 1
    pagenum = scribus.pageCount()
    T = []
    content = []
    T.append('<?xml version="1.0" encoding="UTF-8"?>\n')
    T.append('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">\n')
    T.append('<html xmlns="http://www.w3.org/1999/xhtml">\n<body>\n')
    T.append('<h2>Scribus ' + str(scribus.scribus_version_info[0]) + '.' + str(scribus.scribus_version_info[1]) + '.' + str(scribus.scribus_version_info[2]) + str(scribus.scribus_version_info[3]) + '</h2>\n')
    while (page <= pagenum):
        scribus.gotoPage(page)
        pagesize = scribus.getPageSize()
        d = scribus.getPageItems()
        strpage = str(page)
        T.append('<h3>Part '+ strpage + '</h3>\n')
        for item in d:
            if (item[1] == 4):
                filtered = '<p>'
                contents = scribus.getAllText(item[0])
                if (contents in content):
                    contents = ''
                else:
                    for char in contents:
                        if (ord(char) == 28):
                            char = ' '
                        elif (ord(char) == 27):
                            char = chr(10)
                        elif (ord(char) == 38):
                            char = '&amp;'
                        elif ((ord(char) == 13) or (ord(char) == 10)):
                            char = "</p>\n<p>"         # the added newline makes the source look better
                        filtered = filtered + char
                    contents = filtered
                T.append('<table border="0" cellspacing="0"><tr><td width="700px"><p>' + contents + '</p>\n')
                T.append('</p></td>\n</tr>\n</table>\n')
                content.append(contents)
            elif (item[1] == 2):
                imgname = scribus.getImageFile(item[0])
                imgsize = scribus.getSize(item[0])
                picwidth = int(700 * imgsize[0]/pagesize[0])
                T.append('<p><table border="0">\n<tr>\n<td><img src="' + imgname + '" width="' + str(picwidth) + 'px" /></td>\n</tr>\n</table></p>\n')
                
        page += 1
    T.append('</body>\n</html>\n')
    output_file = open(textfile,'w')
    output_file.writelines(T)
    output_file.close()
    endmessage = textfile + ' was created'
    scribus.messageBox("Finished", endmessage,icon=scribus.ICON_NONE,button1=scribus.BUTTON_OK) # Formerly, you could use icon =1 and button1 = 1


if scribus.haveDoc():
    textfile = scribus.fileDialog('Enter name of file to save to', \
                                  filter='Text Files (*.xhtml);;All Files (*)')
    try:
        if textfile == '':
            raise Exception
        exportText(textfile)
    except Exception, e:
        print e

else:
    scribus.messageBox('Export Error', 'You need a Document open.', \
                       icon=scribus.ICON_NONE, button1=scribus.BUTTON_OK)