0

I would like to edit .docx documents using Python and more importantly I need it to be able to allow me to write subscripts like 2x in Python.

I have tried the docx module but I can't seem to find out how to write subscripts.

Jawa
  • 2,336
  • 6
  • 34
  • 39
acorn19
  • 15
  • 7

2 Answers2

0

If you are trying to write subscripts, try using the "stringified" form, so to speak:

>>> print '2\xe2\x82\x93'
2ₓ
>>> 
A.J. Uppal
  • 19,117
  • 6
  • 45
  • 76
  • ValueError: All strings must be XML compatible: Unicode or ASCII, no NULL bytes or control characters (it does not work :() – acorn19 Mar 30 '15 at 12:09
0

You can use the XML representation of a subscript, eg:

<w:p>
  <w:pPr>
    <w:pStyle w:val="Normal"/>
    <w:rPr>
      <w:shd w:fill="auto" w:val="clear"/>
      <w:vertAlign w:val="subscript"/>
    </w:rPr>
  </w:pPr>
  <w:r>
    <w:rPr/>
    <w:t>2</w:t>
  </w:r>
  <w:r>
    <w:rPr>
      <w:shd w:fill="auto" w:val="clear"/>
      <w:vertAlign w:val="subscript"/>
    </w:rPr>
    <w:t>x</w:t>
  </w:r>
</w:p>

To insert that xml inside your doc, you can use the docxtemplater cli (it is written in Node) https://github.com/open-xml-templating/docxtemplater (I'm maintaining this library)

edi9999
  • 19,701
  • 13
  • 88
  • 127