Skip to content

Instantly share code, notes, and snippets.

@iarp
Created March 5, 2014 01:25
Show Gist options
  • Save iarp/9359428 to your computer and use it in GitHub Desktop.
Save iarp/9359428 to your computer and use it in GitHub Desktop.
Convert Excel tables into MediaWiki formatted tables.
import os
# Open pasted-data.txt file, read the contents.
# You could potentially change this to reading the clipboard, and then writing the clipboard with the table.
with open('pasted-data.txt', 'r') as text:
data = text.readlines()
# If the first line is blank, remove it and set blank to True.<br /># If data[0] throws an error, we were passed nothing. Exit.
blank = None
try:
if not data[0].strip('\r\n').strip('\n').strip('\t'):
del(data[0])
blank = True
except IndexError:
exit()
# Strip the lines of system newlines and then split on tabs
# Add the split line to the table.
table = []
for line in filter(None, data):
line = line.strip('\r\n').strip('\n').split('\t')
table.append([x.strip() for x in line])
# We need to know the longest list compared to all other lines.
# Go through each line, find the longest list.
maximum = max(len(line) for line in table)
# Go through each line, if the line isn't as long as the maximum, append blanks.
for index, line in enumerate(table):
if len(line) &lt; maximum:
table[index].extend(['']*(maximum-len(line)))
# Start building the actual MediaWiki table.
output = ['{| class="wikitable" ']
for index, items in enumerate(table):
# If the table line index is 0, we're processing the first line
# If blank is not True, it means we have header data
if not blank and not index:
# Process the header data
temp = []
for title in items:
temp.append("! align=\"center\" | '''%s'''" % title)
output.append("\n".join(temp))
else:
# This is the default format required for a standard MediaWiki table row.
output.append('|-')
output.append('| ' + ' || '.join(items))
# Add the closing tag
output.append('|}')
# Join the lines by regular newline character
output = '\n'.join(output)
print output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment