I am currently trying to add a checkbox to a word document using the python-docx library. I've narrowed down the checkbox XML to two possible options, the first from evaluating the word/document.xml file from an existing doc and the second from the XML Schema. When trying to insert the XML element into a simple document I receive the error; "lxml.etree.XMLSyntaxError: Namespace prefix xsd on complexType is not defined".
This is what I'm currently trying (using XML from the Schema):
def word_docs(emails):
cbox = parse_xml('<xsd:complexType name="CT_FFCheckBox"><xsd:sequence> \
<xsd:choice><xsd:element name="size"type="CT_HpsMeasure"/> \
<xsd:element name="sizeAuto" type="CT_OnOff"/></xsd:choice> \
<xsd:element name="default" type="CT_OnOff" minOccurs="0"/> \
<xsd:element name="checked" type="CT_OnOff" minOccurs="0"/> \
</xsd:sequence></xsd:complexType>')
doc = Document()
title = doc.add_heading("Document", 0)
table = doc.add_table(rows = 1, cols = 4)
table.style = 'TableGrid'
row = table.rows[0]
row.cells[0].text = "Test"
merged = (row.cells[1].merge(row.cells[2]))
merged._tc._add_p()
....
The following is the XML pulled from an existing document:
<w:tc>
<w:tcPr>
<w:tcW w:w="4788" w:type="dxa"/>
</w:tcPr>
<w:p wsp:rsidR="00834643" wsp:rsidRPr="00834643" wsp:rsidRDefault="00F12FD5" wsp:rsidP="00834643">
<w:pPr>
<w:spacing w:after="0" w:line="240" w:line-rule="auto"/>
</w:pPr>
<w:r>
<w:fldChar w:fldCharType="begin">
<w:fldData xml:space="preserve">/////2UAAAAUAAYAQwBoAGUAYwBrADEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA</w:fldData>
</w:fldChar>
</w:r>
<aml:annotation aml:id="1" w:type="Word.Bookmark.Start" w:name="Check2"/>
<w:r>
<w:instrText> FORMCHECKBOX </w:instrText>
</w:r>
<w:r>
<w:fldChar w:fldCharType="end"/>
</w:r>
<aml:annotation aml:id="1" w:type="Word.Bookmark.End"/>
</w:p>
I've been able to manually add the namespace xmlns:xsd="http://ift.tt/tphNwY"
manually to a document and it seems to open correctly, I am just unsure of how to do this in a pythonic way to automate the process. The XML object manipulation through python-docx may be incorrect, but it is what makes sense to me after comparing the XML format and the python-docx objects and the way they are handled - I haven't been able to test it with this error.
Any help is appreciated! Thanks!
Aucun commentaire:
Enregistrer un commentaire