Example #1
0
def set_metadata(stream, mi):
    from calibre.utils.zipfile import safe_replace
    c = DOCX(stream, extract=False)
    dp_name, ap_name = c.get_document_properties_names()
    dp_raw = c.read(dp_name)
    try:
        ap_raw = c.read(ap_name)
    except Exception:
        ap_raw = None
    cp = etree.fromstring(dp_raw)
    update_doc_props(cp, mi)
    replacements = {}
    if ap_raw is not None:
        ap = etree.fromstring(ap_raw)
        comp = ap.makeelement('{%s}Company' % namespaces['ep'])
        for child in tuple(ap):
            if child.tag == comp.tag:
                ap.remove(child)
        comp.text = mi.publisher
        ap.append(comp)
        replacements[ap_name] = BytesIO(xml2str(ap))
    stream.seek(0)
    safe_replace(stream,
                 dp_name,
                 BytesIO(xml2str(cp)),
                 extra_replacements=replacements)
Example #2
0
def set_metadata(stream, mi):
    from calibre.utils.zipfile import safe_replace
    c = DOCX(stream, extract=False)
    dp_name, ap_name = c.get_document_properties_names()
    dp_raw = c.read(dp_name)
    try:
        ap_raw = c.read(ap_name)
    except Exception:
        ap_raw = None
    cp = etree.fromstring(dp_raw)
    update_doc_props(cp, mi)
    replacements = {}
    if ap_raw is not None:
        ap = etree.fromstring(ap_raw)
        comp = ap.makeelement('{%s}Company' % namespaces['ep'])
        for child in tuple(ap):
            if child.tag == comp.tag:
                ap.remove(child)
        comp.text = mi.publisher
        ap.append(comp)
        replacements[ap_name] = BytesIO(xml2str(ap))
    stream.seek(0)
    safe_replace(stream, dp_name, BytesIO(xml2str(cp)), extra_replacements=replacements)