def __init__(self, opts, log): self.namespace = DOCXNamespace() namespaces = self.namespace.namespaces self.opts, self.log = opts, log self.document_relationships = DocumentRelationships(self.namespace) self.font_table = etree.Element('{%s}fonts' % namespaces['w'], nsmap={k:namespaces[k] for k in 'wr'}) E = ElementMaker(namespace=namespaces['pr'], nsmap={None:namespaces['pr']}) self.embedded_fonts = E.Relationships() self.fonts = {} self.images = {}
def __init__(self, path_or_stream, log=None, extract=True): self.docx_is_transitional = True stream = path_or_stream if hasattr(path_or_stream, 'read') else open(path_or_stream, 'rb') self.name = getattr(stream, 'name', None) or '<stream>' self.log = log or default_log if extract: self.extract(stream) else: self.init_zipfile(stream) self.read_content_types() self.read_package_relationships() self.namespace = DOCXNamespace(self.docx_is_transitional)
def create_skeleton(opts, namespaces=None): namespaces = namespaces or DOCXNamespace().namespaces def w(x): return '{%s}%s' % (namespaces['w'], x) dn = {k:v for k, v in iteritems(namespaces) if k in {'w', 'r', 'm', 've', 'o', 'wp', 'w10', 'wne', 'a', 'pic'}} E = ElementMaker(namespace=dn['w'], nsmap=dn) doc = E.document() body = E.body() doc.append(body) width, height = page_size(opts) width, height = int(20 * width), int(20 * height) def margin(which): val = page_margin(opts, which) return w(which), str(int(val * 20)) body.append(E.sectPr( E.pgSz(**{w('w'):str(width), w('h'):str(height)}), E.pgMar(**dict(map(margin, 'left top right bottom'.split()))), E.cols(**{w('space'):'720'}), E.docGrid(**{w('linePitch'):"360"}), )) dn = {k:v for k, v in iteritems(namespaces) if k in tuple('wra') + ('wp',)} E = ElementMaker(namespace=dn['w'], nsmap=dn) styles = E.styles( E.docDefaults( E.rPrDefault( E.rPr( E.rFonts(**{w('asciiTheme'):"minorHAnsi", w('eastAsiaTheme'):"minorEastAsia", w('hAnsiTheme'):"minorHAnsi", w('cstheme'):"minorBidi"}), E.sz(**{w('val'):'22'}), E.szCs(**{w('val'):'22'}), E.lang(**{w('val'):'en-US', w('eastAsia'):"en-US", w('bidi'):"ar-SA"}) ) ), E.pPrDefault( E.pPr( E.spacing(**{w('after'):"0", w('line'):"276", w('lineRule'):"auto"}) ) ) ) ) return doc, styles, body