1

I am importing some data from xslx file to Django model.

Everything works perfectly except that if cell have some formatting of "superscript" or "subscript" it loses that formatting.

Is there a way to preserve the style formatting of the value in the cell?

m2 becomes simply m2

old-willow
  • 111
  • 1
  • 12
  • There is no formating *superscript*? I think it's `unicode` encoded, looks like django looses `unicode` encoding. – stovfl Mar 11 '17 at 08:50
  • I am not sure what do you mean by unicode loss. If I open the xml in xlsx file I see the tag vertAlign val="superscript". – old-willow Mar 11 '17 at 17:38
  • Possible duplicate of [Editing workbooks with rich text in openpyxl](http://stackoverflow.com/questions/28774757/editing-workbooks-with-rich-text-in-openpyxl) – Charlie Clark Mar 13 '17 at 09:11

1 Answers1

2

If I open the xml in xlsx file I see the tag vertAlign val="superscript"

Comments: I have to use Python 2 and openpyxl 2.2. And the cell value is not just '2' or '3' it is 'm^2' or 'm^3' or what ever,

This is Character Format, you can't pass Character Format to Django. Only the value which ist ^2 or ^3.

For instance this would help:

import re
if vertAlign == superscript:
    value = re.sub('\^2', '²', cell.value)
    value = re.sub('\^3', '³', value)

This changes all simulated superscipt(2|3), to unicode(²|³).

Tested with Python:3.4.2 - openpyxl:2.4.1 - LibreOffice: 4.3.3.2
Should also work with your Python 2 and openpyxl 2.2.

stovfl
  • 14,998
  • 7
  • 24
  • 51
  • Thanks for the effort, but this doesn't work for me. I have to use Python 2 and openpyxl 2.2. And the cell value is not just '2' or '3' it is 'm^2' or 'm^3' or what ever, it can contain more then 2 characters along with formatting only one of them. It looks like I have to write a custom parser for this. – old-willow Mar 11 '17 at 23:25
  • This has nothing to do with the version of openpyxl being used. – Charlie Clark Mar 13 '17 at 09:10