Skip to content

Instantly share code, notes, and snippets.

@prschmid
Created February 18, 2015 21:19
Show Gist options
  • Save prschmid/e59c78dd5af480dfb7b7 to your computer and use it in GitHub Desktop.
Save prschmid/e59c78dd5af480dfb7b7 to your computer and use it in GitHub Desktop.
Simple script to convert an Excel file to MediaWiki format
import sys
import xlrd
if __name__ == '__main__':
if not len(sys.argv) > 1:
sys.exit("Usage: xl2wiki infile.xls [outfile]")
# Read it in and build up a string in the mediawiki format
book = xlrd.open_workbook(sys.argv[1])
s = u""
# Loop over all sheets
for sheet in book.sheets():
s += u"=={}==\n".format(sheet.name)
s += u'{| class="wikitable"\n'
for row_index in xrange(sheet.nrows):
s += u"|-\n"
for col_index in xrange(sheet.ncols):
s += u"| {}\n".format(sheet.cell(row_index, col_index).value)
s += u'|}\n'
# Save it or print it
if len(sys.argv) == 3:
with open(sys.argv[2], 'w') as f:
f.write(s.encode('utf-8'))
else:
print s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment