Properly rename stylename and parentstylename on both automaticstyles and styles.

This commit is contained in:
Jacques Wagener 2018-02-15 18:57:09 +02:00
parent bc312f496a
commit f0c4bd6694
No known key found for this signature in database
GPG Key ID: 6ADD11FE56C539DC
5 changed files with 51 additions and 55 deletions

Binary file not shown.

94
main.py
View File

@ -15,7 +15,7 @@ from apiclient.http import MediaIoBaseDownload
# ODF # ODF
import zipfile, xml.dom.minidom import zipfile, xml.dom.minidom
from odf.opendocument import OpenDocumentText, load from odf.opendocument import OpenDocumentText, load
from odf import text, teletype from odf import office, text, teletype
from odf.element import Text from odf.element import Text
from odf.text import P from odf.text import P
from odf.style import Style, TextProperties, ParagraphProperties from odf.style import Style, TextProperties, ParagraphProperties
@ -76,50 +76,22 @@ def print_e(el, indent=0):
print_e(el, indent) print_e(el, indent)
def merge(inputfile, textdoc, document_id): def rename_style(style, style_renaming, document_id):
style_renaming = {} if style.tagName == 'style:style':
inputtextdoc = load(inputfile) # Rename style, and parent style.
# print_e(inputtextdoc.automaticstyles) for style_type in ['name', 'parentstylename']:
# print('&&&&&&&&&&&') attr_name = style.getAttribute(style_type)
# print_e(inputtextdoc.body) if attr_name:
# return
# import ipdb; ipdb.set_trace()
# Need to make a copy of the list because addElement unlinks from the original
for meta in inputtextdoc.meta.childNodes[:]:
textdoc.meta.addElement(meta)
def rename_style(style):
attr_name = style.getAttribute('name')
new_attr_name = "%s_doc%s" % (attr_name, document_id) new_attr_name = "%s_doc%s" % (attr_name, document_id)
print(attr_name, new_attr_name) style.setAttribute(style_type, new_attr_name)
style.setAttribute('name', new_attr_name)
style_renaming[attr_name] = new_attr_name style_renaming[attr_name] = new_attr_name
return style return style
for style in inputtextdoc.styles.childNodes[:]:
textdoc.styles.addElement(style)
for autostyle in inputtextdoc.automaticstyles.childNodes[:]: def replace_style(el, style_renaming, document_id):
textdoc.automaticstyles.addElement(rename_style(autostyle))
for masterstyles in inputtextdoc.masterstyles.childNodes[:]: if el.attributes:
textdoc.masterstyles.addElement(masterstyles)
for font in inputtextdoc.fontfacedecls.childNodes[:]:
textdoc.fontfacedecls.addElement(font)
for scripts in inputtextdoc.scripts.childNodes[:]:
textdoc.scripts.addElement(scripts)
for settings in inputtextdoc.settings.childNodes[:]:
textdoc.settings.addElement(settings)
def replace_style(el):
if not el.attributes:
return el
# print(el.attributes)
stylename = el.attributes.get(('urn:oasis:names:tc:opendocument:xmlns:text:1.0', 'style-name')) stylename = el.attributes.get(('urn:oasis:names:tc:opendocument:xmlns:text:1.0', 'style-name'))
if stylename and style_renaming.get(stylename): if stylename and style_renaming.get(stylename):
print('Replacing style-name: %s with %s' % (stylename, style_renaming.get(stylename))) print('Replacing style-name: %s with %s' % (stylename, style_renaming.get(stylename)))
@ -133,16 +105,46 @@ def merge(inputfile, textdoc, document_id):
style_renaming[parent_stylename] style_renaming[parent_stylename]
el.childNodes = [ el.childNodes = [
replace_style(x) for x in el.childNodes[:] replace_style(x, style_renaming, document_id) for x in el.childNodes[:]
] ]
return el return el
for body in inputtextdoc.body.childNodes[:]:
b = replace_style(body)
textdoc.body.addElement(body)
textdoc.Pictures = {**textdoc.Pictures, **inputtextdoc.Pictures} def merge(inputfile, textdoc, document_id):
style_renaming = {}
inputtextdoc = load(inputfile)
# Need to make a copy of the list because addElement unlinks from the original
for meta in inputtextdoc.meta.childNodes[:]:
textdoc.meta.addElement(meta)
for autostyle in inputtextdoc.automaticstyles.childNodes[:]:
s = rename_style(autostyle, style_renaming, document_id)
textdoc.automaticstyles.addElement(s)
for style in inputtextdoc.styles.childNodes[:]:
s = rename_style(style, style_renaming, document_id)
textdoc.styles.addElement(style)
for masterstyles in inputtextdoc.masterstyles.childNodes[:]:
textdoc.masterstyles.addElement(masterstyles)
for font in inputtextdoc.fontfacedecls.childNodes[:]:
textdoc.fontfacedecls.addElement(font)
for scripts in inputtextdoc.scripts.childNodes[:]:
textdoc.scripts.addElement(scripts)
for settings in inputtextdoc.settings.childNodes[:]:
textdoc.settings.addElement(settings)
for body in inputtextdoc.body.childNodes[:]:
b = replace_style(body, style_renaming, document_id)
textdoc.body.addElement(b)
textdoc.Pictures.update(inputtextdoc.Pictures)
return textdoc return textdoc
@ -195,12 +197,9 @@ def main():
out.write(doc_stream.getvalue()) out.write(doc_stream.getvalue())
book_of_status = OpenDocumentText() book_of_status = OpenDocumentText()
# book_of_status = load(doc_stream)
# withbreak = Style(name="WithBreak", parentstylename="Standard", family="paragraph") # withbreak = Style(name="WithBreak", parentstylename="Standard", family="paragraph")
# withbreak.addElement(ParagraphProperties(breakbefore="page")) # withbreak.addElement(ParagraphProperties(breakbefore="page"))
# book_of_status.automaticstyles.addElement(withbreak) # book_of_status.automaticstyles.addElement(withbreak)
# page_seperator = P(stylename=withbreak,text=u'')
document_id = 0 document_id = 0
for item in items[:]: for item in items[:]:
@ -210,9 +209,6 @@ def main():
with open(doc_file, 'wb') as out: with open(doc_file, 'wb') as out:
out.write(doc_stream.getvalue()) out.write(doc_stream.getvalue())
# TODO unclear if this is the best approach
# book_of_status.text.addElement(page_seperator)
book_of_status.text.addElement(text.SoftPageBreak())
book_of_status = merge(doc_stream, book_of_status, str(document_id)) book_of_status = merge(doc_stream, book_of_status, str(document_id))
document_id += 1 document_id += 1

Binary file not shown.

Binary file not shown.

Binary file not shown.