72 lines
2.4 KiB
Python
72 lines
2.4 KiB
Python
# -*- coding: utf-8 -*-
|
|
import xdrlib, sys
|
|
import xlrd
|
|
import os
|
|
import csv
|
|
import os.path
|
|
|
|
def isinttype(val):
|
|
return type(val) == type(1.0) and val == int(val)
|
|
|
|
def getTitleValue(titles, fields, key):
|
|
for i in range(len(titles)):
|
|
if titles[i] == key:
|
|
return fields[i]
|
|
assert False
|
|
|
|
table_struct_def = {
|
|
|
|
}
|
|
|
|
def convertSheet(sourceFile, sheet):
|
|
if sheet.name[0] in ('_', '#'):
|
|
return
|
|
if sheet.name in ('Sheet1', 'Sheet2'):
|
|
return
|
|
|
|
outFile = "./" + sheet.name + '@' + sourceFile[:-5] + ".php"
|
|
csvfile = open(outFile, "w")
|
|
csvline = '<' + '?' + 'php'
|
|
csvfile.write(csvline + "\n")
|
|
csvline = 'return' + ' array' + '('
|
|
csvfile.write(csvline + "\n")
|
|
for m in range(2, sheet.nrows):
|
|
csvline = ''
|
|
if sheet.name in table_struct_def:
|
|
primary_key = table_struct_def[sheet.name]['primary_key'](sheet.row_values(3), sheet.row_values(m))
|
|
csvline = primary_key + '=>' + 'array' + '('
|
|
else:
|
|
csvline = str(int(sheet.row_values(m)[0])) + '=>' + 'array' + '('
|
|
csvfile.write(csvline + "\n")
|
|
for n in range(0, sheet.ncols):
|
|
fieldvalue = sheet.row_values(m)[n]
|
|
titleValue = sheet.row_values(1)[n]
|
|
if isinttype(fieldvalue):
|
|
csvline = "'" + titleValue + "'" + '=>' + str(fieldvalue) + ','
|
|
else:
|
|
csvline = "'" + titleValue + "'" + '=>"' + str(fieldvalue) + '",'
|
|
csvfile.write(csvline + "\n")
|
|
csvline = ')' + ','
|
|
csvfile.write(csvline + "\n")
|
|
csvline = ')' + ';'
|
|
csvfile.write(csvline + "\n")
|
|
csvline = '?' + '>'
|
|
csvfile.write(csvline + "\n")
|
|
csvfile.close()
|
|
|
|
def convert(sourceFile):
|
|
if sourceFile == 'PlatformIds.xlsx':
|
|
return
|
|
print(sourceFile)
|
|
excl = xlrd.open_workbook(sourceFile)
|
|
for sheet in excl.sheets():
|
|
convertSheet(sourceFile, sheet)
|
|
|
|
def main():
|
|
for file in os.listdir('./'):
|
|
if file[0] != '~' and (file[-5:] == '.xlsx' or file[-5:] == '.xlsx'):
|
|
convert(file)
|
|
|
|
if __name__ == "__main__":
|
|
main()
|