-1

I am trying to parse excel file which will then create JSON after parsing excel data. The excel may contain words using style like bold, italic color etc. I am getting the formatting using openpyxl but its for whole cell. Unable to get style if cell contains sentence in which one word is bold/italic or colored.

It will be really helpful if anyone can guide me on this

Thanks in advance

PHP_RIDER
  • 355
  • 1
  • 12
  • Possible duplicate of [Editing workbooks with rich text in openpyxl](https://stackoverflow.com/questions/28774757/editing-workbooks-with-rich-text-in-openpyxl) – Charlie Clark Jun 27 '19 at 08:49
  • might not work... https://stackoverflow.com/questions/49522658/openpyxl-change-one-words-color-in-the-same-cell – Joe Jun 27 '19 at 09:33
  • https://bitbucket.org/openpyxl/openpyxl/pull-requests/224/support-of-rich-text-in-cells/diff – Joe Jun 27 '19 at 09:33
  • https://groups.google.com/forum/#!searchin/openpyxl-users/rich$20text|sort:date/openpyxl-users/eFfAsdaTm8k/AUjIxflS4g0J – Joe Jun 27 '19 at 09:34
  • @CharlieClark i need while reading from excel file – PHP_RIDER Jun 27 '19 at 10:30
  • It's no supported as the linked question says. You **could** quite easily extend the library to do so but that's up to you. – Charlie Clark Jun 27 '19 at 10:32

1 Answers1

0

The only and rather tedious way I can think of is extract the xlsx-file (it is only a zip archive) and modify the sharedStrings.xml in there (e.g. using ElementTree). Afterwards zip the file back together.

The approach is described here:

https://stackoverflow.com/a/53454150/7919597

E.g. for a cell like

enter image description here

the sharedStrings.xml looks like

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  <sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" count="1" uniqueCount="1">
    <si>
      <r>
        <rPr>
          <sz val="11"/><color rgb="FFFF0000"/><rFont val="Calibri"/><family val="2"/>
          <scheme val="minor"/>
         </rPr>
         <t xml:space="preserve">Some Text</t>
      </r>

      <r>
        <rPr>
           <sz val="11"/><color theme="7" tint="-0.249977111117893"/><rFont val="Calibri"/><family val="2"/><scheme val="minor"/>
         </rPr>
       <t>Another Text</t>

    </r>
  </si>
</sst>
Joe
  • 6,758
  • 2
  • 26
  • 47
  • Thanks for the help but i am trying to parse and read from excel in that i am getting only one format not the 2nd words format "Another text". Need that. – PHP_RIDER Jun 27 '19 at 12:36