2 # -*- coding: utf-8 -*-
5 # This file is part of LyX, the document processor.
6 # Licence details can be found in the file COPYING.
11 # Full author contact details are available in file CREDITS
13 # This script reads a csv-table (file name.csv) and converts it into
14 # a LyX-table for versions 1.5.0 and higher (LyX table format 276).
15 # It uses Python's csv module for parsing.
16 # The original csv2lyx was witten by Antonio Gulino <antonio.gulino@tin.it>
17 # in Perl for LyX 1.x and modified for LyX table format 276 by the author.
19 import csv, unicodedata
24 sys.stderr.write(message + '\n')
27 header = """#csv2lyx created this file
35 \\font_typewriter default
36 \\font_default_family default
42 \\paperfontsize default
49 \\paperorientation portrait
52 \\paragraph_separation indent
56 \\paperpagestyle default
57 \\tracking_changes false
58 \\output_changes false
63 \\begin_layout Standard
66 <lyxtabular version="3" rows="%d" columns="%d">
70 cell = """<cell alignment="left" valignment="top" usebox="none">
73 \\begin_layout Standard
80 footer = """</lyxtabular>
91 # processing command line options
92 # delegate this to standard module optparse
94 args["usage"] = "Usage: csv2lyx [options] csvfile [file.lyx]"
96 args["description"] = """This script creates a LyX document containing a table created from a
97 comma-separated-value (CSV) file. The resulting LyX file can be opened
98 with LyX 1.5.0 or any later version.
99 If no options are given csv2lyx will try to infer the CSV type of the csvfile,
101 parser = optparse.OptionParser(**args)
103 parser.set_defaults(excel='', column_sep='')
104 parser.add_option("-e", "--excel", metavar="CHAR",
105 help="""CHAR corresponds to a CSV type:
106 'e': Excel-generated CSV file
107 't': Excel-generated TAB-delimited CSV file""")
108 parser.add_option("-s", "--separator", dest="column_sep",
109 help= """column separator
112 group = optparse.OptionGroup(parser, "Remarks", """If your CSV file contains special characters (e. g. umlauts,
113 accented letters, etc.) make sure it is coded in UTF-8 (unicode).
114 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.""")
115 parser.add_option_group(group)
117 (options, args) = parser.parse_args()
125 fout = open(args[1], 'w')
130 if not os.path.exists(infile):
131 error('File "%s" not found.' % infile)
133 dialects = {'' : None, 'e' : 'excel', 't' : 'excel-tab'}
134 if options.excel not in dialects:
137 dialect= dialects[options.excel]
139 # Set Tab, if necessary
140 if options.column_sep == 't':
141 options.column_sep = "\t"
143 # when no special column separator is given, try to detect it:
144 if options.column_sep or dialect :
145 reader = csv.reader(open(infile, "rb"), dialect= dialect, delimiter=options.column_sep)
147 guesser = csv.Sniffer()
148 input_file = "".join(open(infile,'rb').readlines())
150 dialect = guesser.sniff(input_file)
151 reader = csv.reader(open(infile, "rb"), dialect= dialect)
153 reader = csv.reader(open(infile, "rb"), dialect= dialect, delimiter=',')
156 num_cols = 1 # max columns
160 num_cols = max(num_cols, len(row))
163 num_rows = reader.line_num # number of lines
166 #####################
169 fout.write(header % (num_rows, num_cols))
171 #####################
174 for i in range(num_cols):
175 fout.write('<column alignment="left" valignment="top" width="0pt">\n')
177 for j in range(num_rows):
180 ############################
181 # write contents of one line
182 ############################
183 for i in range(len(rows[j])):
184 row.append( cell % rows[j][i])
186 # If row has less columns than num_cols fill with blank entries
187 for i in range(len(rows[j]), num_cols):
188 row.append(cell % " ")
190 fout.write("\n".join(row) + '\n</row>\n')
192 #####################