Make certain characters in an Excel cell subscript Python

0 votes

I'm attempting to create a Python script that subscripts or superscripts specific characters in an Excel cell's content. I was using OpenPyxl, but I've read that it only permits changes that affect the entire cell, so if that's the case, I'm willing to switch to another package.

I need each cell's two characters that follow the "T" to be a subscript for reference. So, format the string "T12+0.10" or "(T12+17.00" so that the "12" is a subscript, as seen in the illustration below.

enter image description here

Here's what I have so far:

style = XFStyle()
style.font = fnt
style.borders = borders
substyle = fnt.ESCAPEMENT_SUBSCRIPT
superstyle = fnt.ESCAPEMENT_SUPERSCRIPT


def compare(contents):
    s = ""
    for i in contents:
        nextchar=contents[i+1].value
        if i.value.contains ("T"):
            j = i + 1, substyle
            s += str(s + j)
        else:
            s += str(i)

wb = op.load_workbook("file_name.xlsx")
ws = wb["Sheet3"]
for row in ws.iter_rows("C{}:C{}".format(ws.min_row, ws.max_row)):
    for cell in row:
        contents = cell.value
        compare(contents)
Dec 13, 2022 in Others by Kithuzzz
• 29,080 points
101 views

1 answer to this question.

0 votes

With Xlwings, you may accomplish this by assuming that the next two characters are subscripted where there is a "T" in the cell contents.

import xlwings as xw

wb = xw.Book('Book1.xlsx')
ws = wb.sheets('Sheet1')
for x in range(1, 6): 
    contents = ws.range(1, x).value 
    if 'T' in contents: # Checks for T in the cell contents
        i = contents.index('T')+1
        ws.range(1, x).characters[i:i+2].api.Font.Subscript = True

wb.save()
wb.close()

As an example, this changes the following cells A1:E1 from

T12+0.10    <(T12+17.00 A12+1.11    A+0.10T12   B14+111

To

enter image description here

answered Dec 13, 2022 by narikkadan
• 53,920 points

Related Questions In Others

0 votes
0 answers

Format an Excel column (or cell) as Text in C#?

When I copy values from a data ...READ MORE

Oct 31, 2022 in Others by Kithuzzz
• 29,080 points
228 views
0 votes
1 answer

Format an Excel Sheet in Python

The below code will help you refer ...READ MORE

answered Nov 6, 2022 in Others by narikkadan
• 53,920 points
210 views
0 votes
1 answer

Update an Excel sheet in real time using Python

Using xlwings, I have really discovered how ...READ MORE

answered Nov 13, 2022 in Others by narikkadan
• 53,920 points
417 views
0 votes
1 answer

Using office scripts to read superScripts characters from a cell in excel online

My understanding is that superscript and subscript ...READ MORE

answered Nov 17, 2022 in Others by narikkadan
• 53,920 points
105 views
0 votes
1 answer

ImportError: openpyxl is required for loading excel format files

Forget the PsychoPy complications for the time ...READ MORE

answered Oct 3, 2018 in Python by Priyaj
• 58,100 points
482 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 3,329 views
0 votes
1 answer

Is there any way in python to auto-correct spelling mistake in multiple rows of an excel files of a single column?

Use Spellchecker for doing your stuff: import pandas ...READ MORE

answered Oct 14, 2022 in Others by narikkadan
• 53,920 points
328 views
0 votes
1 answer

How to stick an embedded document in a specific cell of an excel

Solution Select the documents (you can use the ...READ MORE

answered Oct 18, 2022 in Others by narikkadan
• 53,920 points
83 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP