Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Convert a space delimited table on the system clipboard into markdown table, such as when copying tabular results from matlab or a spreadsheet or R command line. Written to use macos shell cmd pbpaste. But should be reworked to use xclip or similar as an option for linux. This script is most enjoyable when combined with a os wide shortcut, such …
#! /usr/bin/env python
#Created by James B. Ackman 4/9/2013
A script to format a space or tab delimited table on the clipboard into a markdown formatted table onto the clipboard
#import os, sys, re
#import urllib2
import os, subprocess, re
def getClipboardData():
p = subprocess.Popen(['pbpaste'], stdout=subprocess.PIPE)
retcode = p.wait()
data =
return data
def setClipboardData(data):
p = subprocess.Popen(['pbcopy'], stdin=subprocess.PIPE)
retcode = p.wait()
s1 = getClipboardData()
s = s1.expandtabs(1)
s = re.sub(' +',' ',s)
sList = s.split('\n')
nSeparators = sList[0].count(' ')
nSeparatorsTotal = s.count(' ')
newStr = s.replace(' ', ' | ', nSeparatorsTotal)
row2 = ' --- |' * nSeparators + ' ---'
newStr2 = newStr.replace('\n', '\n' + row2 + '\n', 1)
#nLine = sList[0].replace(' ', ' | ', nSeparators)
#print str(row2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.