2 # -*- coding: utf-8 -*-
5 # This file is part of LyX, the document processor.
6 # Licence details can be found in the file COPYING.
10 # Full author contact details are available in file CREDITS
12 # This script reads a csv-table (file name.csv) and converts it into
13 # a LyX-table for versions 1.5.0 and higher (LyX table format 276).
14 # It uses Python's csv module for parsing.
15 # The original csv2lyx was witten by Antonio Gulino <antonio.gulino@tin.it>
16 # in Perl for LyX 1.x and modified for LyX table format 276 by the author.
18 import csv, unicodedata
23 sys.stderr.write(message + '\n')
26 header = """#csv2lyx created this file
34 \\font_typewriter default
35 \\font_default_family default
41 \\paperfontsize default
48 \\paperorientation portrait
51 \\paragraph_separation indent
55 \\paperpagestyle default
56 \\tracking_changes false
57 \\output_changes false
62 \\begin_layout Standard
65 <lyxtabular version="3" rows="%d" columns="%d">
69 cell = """<cell alignment="left" valignment="top" usebox="none">
72 \\begin_layout Standard
79 footer = """</lyxtabular>
90 # processing command line options
91 # delegate this to standard module optparse
93 args["usage"] = "Usage: csv2lyx [options] csvfile [file.lyx]"
95 args["description"] = """This script creates a LyX document containing a table created from a
96 comma-separated-value (CSV) file. The resulting LyX file can be opened
97 with LyX 1.5.0 or any later version.
98 If no options are given csv2lyx will try to infer the CSV type of the csvfile,
100 parser = optparse.OptionParser(**args)
102 parser.set_defaults(excel ='', column_sep = '')
103 parser.add_option("-e", "--excel", metavar ="CHAR",
104 help = """CHAR corresponds to a CSV type:
105 'e': Excel-generated CSV file
106 't': Excel-generated TAB-delimited CSV file""")
107 parser.add_option("-s", "--separator", dest = "column_sep",
108 help = """column separator
111 group = optparse.OptionGroup(parser, "Remarks", """If your CSV file contains special characters (e. g. umlauts,
112 accented letters, etc.) make sure it is coded in UTF-8 (unicode).
113 Else LyX will loose some cell contents. If your CSV file was not written according to the "Common Format and MIME Type for Comma-Separated Values (CSV) Files" (http://tools.ietf.org/html/rfc4180) there may be unexpected results.""")
114 parser.add_option_group(group)
116 (options, args) = parser.parse_args()
124 fout = open(args[1], 'w')
129 if not os.path.exists(infile):
130 error('File "%s" not found.' % infile)
132 dialects = {'' : None, 'e' : 'excel', 't' : 'excel-tab'}
133 if options.excel not in dialects:
136 dialect = dialects[options.excel]
138 # Set Tab, if necessary
139 if options.column_sep == 't':
140 options.column_sep = "\t"
142 # when no special column separator is given, try to detect it:
143 if options.column_sep and dialect :
144 reader = csv.reader(open(infile, "rb"), dialect = dialect, delimiter = options.column_sep)
146 guesser = csv.Sniffer()
147 input_file = "".join(open(infile,'rb').readlines())
149 dialect = guesser.sniff(input_file)
150 reader = csv.reader(open(infile, "rb"), dialect = dialect)
152 # older versions (python < 2.5) of csv have problems (bugs)
153 # that is why we try harder to get a result, this should work on most cases
154 # as it assumes that the separator is a comma (the c in csv :-) )
156 reader = csv.reader(open(infile, "rb"), dialect = dialect, delimiter = ',')
158 reader = csv.reader(open(infile, "rb"), delimiter = ',')
161 num_cols = 1 # max columns
165 num_cols = max(num_cols, len(row))
168 num_rows = len(rows) # number of lines
171 #####################
174 fout.write(header % (num_rows, num_cols))
176 #####################
179 for i in range(num_cols):
180 fout.write('<column alignment="left" valignment="top" width="0pt">\n')
182 for j in range(num_rows):
185 ############################
186 # write contents of one line
187 ############################
188 for i in range(len(rows[j])):
189 row.append( cell % rows[j][i].replace('\\','\\backslash\n'))
191 # If row has less columns than num_cols fill with blank entries
192 for i in range(len(rows[j]), num_cols):
193 row.append(cell % " ")
195 fout.write("\n".join(row) + '\n</row>\n')
197 #####################