Skip to content

Instantly share code, notes, and snippets.

@kspeeckaert
Created March 14, 2016 15:08
Show Gist options
  • Save kspeeckaert/1a4c92e5ac5ed57b74a4 to your computer and use it in GitHub Desktop.
Save kspeeckaert/1a4c92e5ac5ed57b74a4 to your computer and use it in GitHub Desktop.
Convert an HTML table (on the clipboard) to Markdown
from bs4 import BeautifulSoup
from tabulate import tabulate
import xerox
raw_html = xerox.paste()
tab_html = BeautifulSoup(raw_html, 'lxml')
rows = []
for row in tab_html.find_all('tr'):
cols = [x.get_text().strip() for x in row.find_all(['td', 'th'])]
rows.append(cols)
tab_md = tabulate(rows, headers='firstrow', tablefmt='pipe')
xerox.copy(tab_md)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment