-1

I'm working on a script with python-docx which create a table(rows=1, cols=1). The text inside the cell need to be vertically and horizontally aligned, with a gray background.

I used WD_TABLE_ALIGNMENT to horizontally align the text and xml to color the background. But here is my problem. I took a XML script to vertically align, and I have an error : "global name 'qn' is not defined."

Here is the code :

from docx import Document
from docx.enum.text import WD_ALIGN_PARAGRAPH
from docx.enum.table import WD_TABLE_ALIGNMENT
from docx.oxml.ns import nsdecls
from docx import parse_xml, OxmlElement

def create_table():
    table = document.add_table(rows=1, cols=1)
    table.alignment = WD_TABLE_ALIGNMENT.CENTER
    row_cells=table.rows[0].cells
    tc=row_cells[0]._tc
    tcPr=tc.get_or_add_tcPr
    tcVAlign = OxmlElement('w:vAlign')
    tcVAlign.set(qn('w:val'), "center")
    tcPr.append(tcVAlign)

Is it an import error ? Or script error ?

Thanks.

  • You are calling a function named `qn` (`tcVAlign.set(qn('w:val'), "center")`). Where is that defined? It doesn't exist in the code you've posted. – larsks Mar 02 '18 at 13:28
  • I don't know, I just copied this code, trying to understand it because I never used XML, and everyone using "qn" like a XML library or whatever. – W. Delahaye Mar 02 '18 at 13:33

1 Answers1

0

I was looking into the library of docx, and in /docx/oxml/xlmchemy.py I found a line :

from .ns import qn

So I tried to do this :

from docx.oxml.ns import qn

which is a ns function and it works.

Thanks.