Search code examples
pythonexcelexcel-2011vba

Replace italics within Excel file with HTML tags


is there a way to replace formatting such as italics with their tags, e.g. ? More in general, how do I search for a format:italic and replace with

<i>foundstring(^&)</i>?

Basically what I want is to have this:

blabla bla bla

replaced with this:

blabla <i>bla</i> bla

This is because I need to export the .xslx to .csv while 'preserving' the formatting. Is there a VBA script which works with MS Office 2011 for Mac? Or can it be don, say, with Python?

Thanks in advance for any help.

Edit: this is the vba code I tried to no avail:

Sub Tag_Italic()

Dim oWS As Worksheet

Dim oRng As Range

Dim FirstUL

Set oWS = ActiveSheet

Application.FindFormat.Clear

Application.FindFormat.Font.Italic = True

Set oRng = oWS.Range("A1:A1000").Find(What:="", LookIn:=xlValues, LookAt:= _

xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, SearchFormat:=True)

If Not oRng Is Nothing Then

FirstUL = oRng.Row

Do

oRng.Font.Italic = False

oRng.Value2 = "" & oRng.Value2 & ""

Set oRng = oWS.Range("A" & CStr(oRng.Row + 1) & ":A1000").Find(What:="", LookIn:=xlValues, LookAt:= _

xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, SearchFormat:=True)

Loop While Not oRng Is Nothing

End If

End Sub

Edit 2: Gary's Student's answer did the job!


Solution

  • Consider the following UDF():

    Public Function TagMaker(r As Range) As String
       Dim outpt As String, v As String, C As String
       Dim i As Long, boo As Boolean
       Dim booNext As Boolean
    
       outpt = ""
       v = r.Text
    
       For i = 1 To Len(v)
          boo = r.Characters(i, 1).Font.Italic
          C = Mid(v, i, 1)
          If (i = 1) And boo Then
             outpt = "<i>"
          End If
          If i = Len(v) Then
             If boo Then
                outpt = outpt & C & "</i>"
                Exit For
             Else
                outpt = outpt & C
                Exit For
             End If
          End If
    
    
          booNext = r.Characters(i + 1, 1).Font.Italic
          If (boo And booNext) Or (Not boo And Not booNext) Then
             outpt = outpt & C
          End If
          If boo And Not booNext Then
             outpt = outpt & C & "</i>"
          End If
          If Not boo And booNext Then
             outpt = outpt & C & "<i>"
          End If
       Next i
    TagMaker = outpt
    End Function
    

    Examples:

    enter image description here