Esempio n. 1
0
def testmakeelement():
    """Ensure custom elements get created"""
    testelement = makeelement("testname", attributes={"testattribute": "testvalue"}, tagtext="testtagtext")
    assert testelement.tag == ("{http://schemas.openxmlformats.org/wordprocessingml/2006/main}testn" "ame")
    assert testelement.attrib == {
        "{http://schemas.openxmlformats.org/wordprocessingml/2006/main}testa" "ttribute": "testvalue"
    }
    assert testelement.text == "testtagtext"
def add_hyperlink(par, url):
    global _addedhyperlinks
    _addedhyperlinks.append(url)
    hl = docx.makeelement(
        'hyperlink',
        attrnsprefix='r',
        attributes={
            'id':
            'rId' + str(len(docx.relationshiplist()) + len(_addedhyperlinks))
        })
    run = docx.makeelement('r')
    rPr = docx.makeelement('rPr')
    rPr.append(docx.makeelement('rStyle', attributes={'val': 'Hyperlink'}))
    run.append(rPr)
    run.append(docx.makeelement('t', tagtext=url))
    hl.append(run)
    par.append(hl)
    return par
Esempio n. 3
0
def testmakeelement():
    """Ensure custom elements get created"""
    testelement = makeelement('testname',
                              attributes={'testattribute': 'testvalue'},
                              tagtext='testtagtext')
    assert testelement.tag == (
        '{http://schemas.openxmlformats.org/wordprocessingml/2006/main}testn'
        'ame')
    assert testelement.attrib == {
        '{http://schemas.openxmlformats.org/wordprocessingml/2006/main}testa'
        'ttribute':
        'testvalue'
    }
    assert testelement.text == 'testtagtext'
    # Add an image
    relationships, picpara = dx.picture(relationships, 'image1.png',
                                        'This is a test description')
    docbody.append(picpara)

    docbody.append(
        dx.paragraph([
            ('hello', {}),
            ('2', {
                'vertAlign': 'superscript'
            }),
        ]))

    # Append a table with special properties and cells
    spec_cell = dx.paragraph([('2', {'vertAlign': 'superscript'})])
    t_prop_margin = dx.makeelement('tblCellMar')
    for margin_type in ['top', 'left', 'right', 'bottom']:
        t_prop_margin.append(
            dx.makeelement(margin_type, attributes={
                'w': '0',
                'type': 'dxa'
            }))
    CELL_SIZE = 12 * 30  # twenties of a point
    docbody.append(
        dx.table(
            [[
                'A1', {
                    'content': spec_cell,
                    'style': {
                        'vAlign': {
                            'val': 'top'
    # Add an image
    relationships, picpara = dx.picture(
        relationships,
        'image1.png',
        'This is a test description')
    docbody.append(picpara)

    docbody.append(dx.paragraph([
        ('hello', {}),
        ('2', {'vertAlign': 'superscript'}),
        ]))

    # Append a table with special properties and cells
    spec_cell = dx.paragraph([('2', {'vertAlign': 'superscript'})])
    t_prop_margin = dx.makeelement('tblCellMar')
    for margin_type in ['top', 'left', 'right', 'bottom']:
        t_prop_margin.append(dx.makeelement(margin_type, attributes={'w': '0', 'type': 'dxa'}))
    CELL_SIZE = 12*30 # twenties of a point
    docbody.append(dx.table([['A1',
                              {'content': spec_cell, 'style': {'vAlign': {'val': 'top'},
                                                               'shd': {'fill': '777777'}}},
                              ('A3', 'ttt')],
                             ['B1','B2','B3'],
                             ['C1','C2','C3']],
        heading=False,
        colw=[CELL_SIZE]*3,
        cwunit='dxa', # twenties of a point
        borders={'all': {'color': 'AAAAAA'}},
        celstyle=[{'align': 'center', 'vAlign': {'val': 'center'}}]*3,
        rowstyle={'height': CELL_SIZE},
def makeref(doc_body, row):
    refid = None
    try:
        refid = int(row["ID"])
    except ValueError:
        pass
    authors = None
    if row[authorskey] and row[authorskey] not in ["", " "]:
        authors = ", ".join([(", ".join([aa.strip() for aa in a.split(",")]))
                             for a in row[authorskey].split(";")])
    cit_a = ((authors + " " if authors else "") +
             ("(" + row[yrkey] + ")" if row[yrkey] else "") + ". " if authors
             else "") + (row[titlekey] + ". " if row[titlekey] else "")
    cit_ab = ""
    cit_b = (row[jfullkey]
             if row[jfullkey] and row[jfullkey] not in ["", " "] else "")
    if cit_b == "":
        sauthors = None
        if row[sauthorskey] and row[sauthorskey] not in ["", " "]:
            sauthors = ", ".join([
                (", ".join([aa.strip() for aa in a.split(",")]))
                for a in row[sauthorskey].split(";")
            ])
        cit_ab = ("In " + sauthors + " (Eds.)" if sauthors else "")
        cit_b = (row[stitlekey]
                 if row[stitlekey] and row[stitlekey] not in ["", " "] else "")
        if cit_ab != "":
            cit_a += cit_ab + (", " if cit_b != "" else "")
    cit_c = (row[volumekey]
             if row[volumekey] and row[volumekey] not in ["", " "] else "") + (
                 (" " if row[volumekey] and row[volumekey] not in ["", " "]
                  else "") + "(" + row[issuekey] + ")"
                 if row[issuekey] and row[issuekey] not in ["", " "] else "")
    cit_c = (cit_c + ", " if cit_c != "" else "") + (
        row[startpagekey] + ("-" + row[otherpagekey] if row[otherpagekey]
                             and row[otherpagekey] not in ["", " "] else "")
        if row[startpagekey] and row[startpagekey] not in ["", " "] else "")
    cit_c = (cit_c + "." if cit_c != "" else "")
    cit_d = ""
    if not (row[jfullkey] and row[jfullkey] not in ["", " "]):
        cit_d = (row[placekey] if row[placekey]
                 and row[placekey] not in ["", " "] else "") + (
                     ": " if row[placekey] and row[placekey] not in ["", " "]
                     and row[publisherkey]
                     and row[publisherkey] not in ["", " "] else
                     "") + (row[publisherkey] if row[publisherkey]
                            and row[publisherkey] not in ["", " "] else "")
    par_list = [(cit_a, '')]
    if cit_b != "":
        par_list.append((cit_b, 'i'))
    par_list.append((((", " if cit_b != "" or cit_ab != "" else "") +
                      cit_c if cit_c != "" else "."), ''))
    if cit_d != "":
        par_list.append((" " + cit_d + ".", ''))
    p = docx.paragraph(par_list)
    # from https://github.com/python-openxml/python-docx/issues/74
    if row[linkskey] and row[linkskey] != "":
        run = docx.makeelement('r')
        rPr = docx.makeelement('rPr')
        run.append(rPr)
        t = docx.makeelement('t', tagtext=" ")
        t.set('{http://www.w3.org/XML/1998/namespace}space', 'preserve')
        run.append(t)
        p.append(run)
        add_hyperlink(p, row[linkskey])
    if row[pidkey] and row[pidkey] != "":
        run = docx.makeelement('r')
        rPr = docx.makeelement('rPr')
        run.append(rPr)
        t = docx.makeelement('t', tagtext=" ")
        t.set('{http://www.w3.org/XML/1998/namespace}space', 'preserve')
        run.append(t)
        p.append(run)
        add_hyperlink(p, doralinkprefix + row[pidkey])
    run = docx.makeelement('r')
    rPr = docx.makeelement('rPr')
    run.append(rPr)
    t = docx.makeelement('t', tagtext=" ")
    t.set('{http://www.w3.org/XML/1998/namespace}space', 'preserve')
    run.append(t)
    p.append(run)
    add_hyperlink(p, (refworksreportlinkprefix if
                      (refid and refid < 10000) else refworkslinkprefix) +
                  row["ID"])
    doc_body.append(p)
    return p