| 4007 |
chandransh |
1 |
#!/usr/bin/python
|
|
|
2 |
|
|
|
3 |
'''
|
|
|
4 |
Tool to convert CSV files (with configurable delimiter) to Excel spreadsheets.
|
|
|
5 |
|
|
|
6 |
This is an adaptation of the script present at
|
|
|
7 |
http://sujitpal.blogspot.com/2007/02/python-script-to-convert-csv-files-to.html
|
|
|
8 |
|
|
|
9 |
The CSV to excel conversion logic has been preserved but the parsing of options
|
|
|
10 |
has been moved to the more recent optparse.
|
|
|
11 |
|
|
|
12 |
Created on 08-Nov-2011
|
|
|
13 |
|
|
|
14 |
@author: Chandranshu
|
|
|
15 |
'''
|
|
|
16 |
import optparse
|
|
|
17 |
import csv
|
|
|
18 |
import string
|
|
|
19 |
import sys
|
|
|
20 |
import os.path
|
|
|
21 |
from pyExcelerator import Workbook
|
|
|
22 |
|
|
|
23 |
def openExcelSheet(outputFileName):
|
|
|
24 |
"""
|
|
|
25 |
Opens a reference to an Excel WorkBook and Worksheet objects
|
|
|
26 |
"""
|
|
|
27 |
workbook = Workbook()
|
|
|
28 |
worksheet = workbook.add_sheet("Sheet 1")
|
|
|
29 |
return workbook, worksheet
|
|
|
30 |
|
|
|
31 |
def writeExcelHeader(worksheet, titleCols):
|
|
|
32 |
"""
|
|
|
33 |
Write the header line into the worksheet
|
|
|
34 |
"""
|
|
|
35 |
cno = 0
|
|
|
36 |
for titleCol in titleCols:
|
|
|
37 |
worksheet.write(0, cno, titleCol)
|
|
|
38 |
cno = cno + 1
|
| 4084 |
chandransh |
39 |
|
|
|
40 |
worksheet.panes_frozen = True
|
|
|
41 |
worksheet.horz_split_pos = 1
|
| 4007 |
chandransh |
42 |
|
|
|
43 |
def writeExcelRow(worksheet, lno, columns):
|
|
|
44 |
"""
|
|
|
45 |
Write a non-header row into the worksheet
|
|
|
46 |
"""
|
|
|
47 |
cno = 0
|
|
|
48 |
for column in columns:
|
|
|
49 |
worksheet.write(lno, cno, column)
|
|
|
50 |
cno = cno + 1
|
|
|
51 |
|
|
|
52 |
def closeExcelSheet(workbook, outputFileName):
|
|
|
53 |
"""
|
|
|
54 |
Saves the in-memory WorkBook object into the specified file
|
|
|
55 |
"""
|
|
|
56 |
workbook.save(outputFileName)
|
|
|
57 |
|
|
|
58 |
def renameOutputFile(outputFileName, fno):
|
|
|
59 |
"""
|
|
|
60 |
Renames the output file name by appending the current file number to it
|
|
|
61 |
"""
|
|
|
62 |
dirName, baseName = os.path.split(outputFileName)
|
|
|
63 |
rootName, extName = os.path.splitext(baseName)
|
|
|
64 |
backupFileBaseName = string.join([string.join([rootName, str(fno)], '-'), extName], '')
|
|
|
65 |
backupFileName = os.path.join(dirName, backupFileBaseName)
|
|
|
66 |
try:
|
|
|
67 |
os.rename(outputFileName, backupFileName)
|
|
|
68 |
except OSError:
|
|
|
69 |
print "Error renaming output file:", outputFileName, "to", backupFileName, "...aborting"
|
|
|
70 |
sys.exit(-1)
|
|
|
71 |
|
|
|
72 |
def getDefaultOutputFileName(inputFileName):
|
|
|
73 |
"""
|
|
|
74 |
Returns the name of the default output file based on the value
|
|
|
75 |
of the input file. The default output file is always created in
|
|
|
76 |
the current working directory. This can be overriden using the
|
|
|
77 |
-o or --output option to explicitly specify an output file
|
|
|
78 |
"""
|
|
|
79 |
baseName = os.path.basename(inputFileName)
|
|
|
80 |
rootName = os.path.splitext(baseName)[0]
|
|
|
81 |
return string.join([rootName, "xls"], '.')
|
|
|
82 |
|
|
|
83 |
def main():
|
|
|
84 |
parser = optparse.OptionParser()
|
|
|
85 |
parser.add_option("-t", "--title", dest="title_present",
|
|
|
86 |
action="store_true",
|
|
|
87 |
help="If set, the first line is the title line")
|
|
|
88 |
parser.add_option("-l", "--lines", dest="lines",
|
|
|
89 |
type="string",
|
|
|
90 |
help="Split output into files of LINE lines or less each.",
|
|
|
91 |
metavar="LINE")
|
|
|
92 |
parser.add_option("-s", "--sep", dest="sep",
|
|
|
93 |
type="string",
|
|
|
94 |
help="Use SEP as a record separator rather",
|
|
|
95 |
metavar="SEP")
|
|
|
96 |
parser.add_option("-o", "--output", dest="outputFileName",
|
|
|
97 |
type="string",
|
|
|
98 |
help="Create the output file with name NAME",
|
|
|
99 |
metavar="NAME")
|
|
|
100 |
parser.set_defaults(title_present=False, lines=-1, sep="\t", outputFileName="")
|
|
|
101 |
parser.set_usage("%prog [options] csvfile")
|
|
|
102 |
(options, args) = parser.parse_args()
|
|
|
103 |
if len(args) != 1:
|
|
|
104 |
parser.error("Please at least provide the input file name")
|
|
|
105 |
|
|
|
106 |
inputFileName = args[0]
|
|
|
107 |
try:
|
|
|
108 |
inputFile = open(inputFileName, 'r')
|
|
|
109 |
except IOError:
|
|
|
110 |
print "File not found:", inputFileName, "...aborting"
|
|
|
111 |
|
|
|
112 |
titlePresent, linesPerFile, sepChar, outputFileName = options.title_present, options.lines, options.sep, options.outputFileName
|
|
|
113 |
if outputFileName == "":
|
|
|
114 |
outputFileName = getDefaultOutputFileName(inputFileName)
|
|
|
115 |
|
|
|
116 |
workbook, worksheet = openExcelSheet(outputFileName)
|
|
|
117 |
fno = 0
|
|
|
118 |
lno = 0
|
|
|
119 |
titleCols = []
|
|
|
120 |
reader = csv.reader(inputFile, delimiter=sepChar)
|
|
|
121 |
for line in reader:
|
|
|
122 |
if lno == 0 and titlePresent:
|
|
|
123 |
if len(titleCols) == 0:
|
|
|
124 |
titleCols = line
|
|
|
125 |
writeExcelHeader(worksheet, titleCols)
|
|
|
126 |
else:
|
|
|
127 |
writeExcelRow(worksheet, lno, line)
|
|
|
128 |
lno = lno + 1
|
|
|
129 |
if (linesPerFile != -1 and lno >= linesPerFile):
|
|
|
130 |
closeExcelSheet(workbook, outputFileName)
|
|
|
131 |
renameOutputFile(outputFileName, fno)
|
|
|
132 |
fno = fno + 1
|
|
|
133 |
lno = 0
|
|
|
134 |
workbook, worksheet = openExcelSheet(outputFileName)
|
|
|
135 |
inputFile.close()
|
|
|
136 |
closeExcelSheet(workbook, outputFileName)
|
|
|
137 |
if (fno > 0):
|
|
|
138 |
renameOutputFile(outputFileName, fno)
|
|
|
139 |
|
|
|
140 |
if __name__ == '__main__':
|
| 4084 |
chandransh |
141 |
main()
|