Excel to MediaWiki Table Script

I just posted an easy to use Excel to MediaWiki Table Generator post in the Tools section. I figured someone might like the actual Python code written to convert the data. It is below.

This is based upon having copied the data from Excel, pasted into a file named pasted-data.txt and then running this script.

import os
import sys

# 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.
# If data[0] throws an error, we were passed nothing. Exit.
blank = None
    if not data[0].strip('\r\n').strip('\n').strip('\t'):
        blank = True
except IndexError:

# 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) < maximum:

# 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)


        # This is the default format required for a standard MediaWiki table row.
        output.append('| ' + ' || '.join(items))

# Add the closing tag

# Join the lines by regular newline character
output = '\n'.join(output)

print output

Clipboard - Copy from Excel & Paste into MediaWiki

An idea that occurred to me while writing this page is, most of the time you'll be working in Excel and MediaWiki. Maybe the step of pasting and saving to the pasted-data.txt file is too much.

How about reading and writing to the clipboard directly.

  • Copy Excel rows
  • Run Python script
  • Paste into MediaWiki directly.

Add this to the very top of the script

from Tkinter import Tk

Replace these lines:

with open('pasted-data.txt', 'r') as text:
    data = text.readlines()

With this:

r = Tk()
data = r.clipboard_get()
data = data.split('\n')

Replace this line:

print output

With this: