0
votes

I am using openpyxl 2.5.12 and python 2.7. I am using a excel template and need to retain the format of headers. There are some superscript values like Summary^2 that just end up as Summary2. I understand that styles are set at a cell level in openpyxl. Is there any other workaround for this? i really need this since there are many headers and footnotes as superscripts

tried setting different styles, but it either makes the whole cell value as superscript or none. Not just the last value as needed

1

1 Answers

0
votes

This code assigns the alignment of your text to superscript which basiclly means "make this cell superscript" to openpyxl

#Import openpyxl
from openpyxl import *

#Get the workbook and set the work sheet
workbook = Workbook()
ws = wb.active

#Add some data
ws.append(['Hello', 'World'])

#Change vertAlign so that the text is superscript
ws['A1'].vertAlign = "superscript"

#Save the workbook
workbook.save('file_name.xlsx')

EDIT: This can be done in xlsxwriter. So what you're looking to do is:

import xlsxwriter

workbook = xlsxwriter.Workbook('rich_strings.xlsx')
worksheet = workbook.add_worksheet()

worksheet.set_column('A:A', 30)
superscript = workbook.add_format({'font_script': 1})
worksheet.write_rich_string('cell',
                            'hello',
                            superscript, 'world'
)
workbook.close()