Converting text to HTML with Python

simple script to add HTML tags

I had to copy/paste a lot of text to make it into my static website

Below is the short script I wrote, to enclose each line of raw text with the <p> tags.

Workflow:
- copy text
- run script (in my case: Cmd + Tab to switch to VS Code, then Cmd + 5 - custom keybinding - to run the script). Note: if used ongoing, script can be run from a key on Streamdeck or with a global keyboard shortcut.
- paste html

from pandas.io.clipboard import clipboard_get
import subprocess

# function to write output to clipboard after processing, ready to past
def write_to_clipboard(output):
    process = subprocess.Popen(
        'pbcopy', env={'LANG': 'en_US.UTF-8'}, stdin=subprocess.PIPE)
    process.communicate(output.encode('utf-8'))

output = ''

# get text from clipboard
text = clipboard_get()
print(f"--- repr(text):\n\n{repr(text)}\n\n")

# split on line breaks
parts = text.split("\n")

# add <p> tags
print(f"--- HTML to copy/paste:\n")
for x in parts:
    if len(x) > 0:
        add = f"<p>{x.strip()}</p>"
        print(add)
        if output == '':
            output = add
        else:
            output = f"{output}\n{add}"
    else:
        continue

# add back to clipboard
write_to_clipboard(output)

links

social