]> git.lyx.org Git - lyx.git/blob - lib/scripts/csv2lyx.py
fe8ba275c79984adb4f0704862c5bd5695d4c680
[lyx.git] / lib / scripts / csv2lyx.py
1 #! /usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 # file csv2lyx.py
5 # This file is part of LyX, the document processor.
6 # Licence details can be found in the file COPYING.
7
8 # author Hartmut Haase
9
10 # Full author contact details are available in file CREDITS
11
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 # The original csv2lyx was witten by Antonio Gulino <antonio.gulino@tin.it>
15 # in Perl for LyX 1.x and modified for LyX table format 276 by the author.
16 #
17
18
19 import os, re, string, sys, unicodedata
20
21 def error(message):
22     sys.stderr.write(message + '\n')
23     sys.exit(1)
24
25 # processing command line options
26 if len(sys.argv) == 1 or sys.argv[1] == '--help':
27     print '''Usage:
28    csv2lyx [options] mycsvfile mytmptable.lyx
29
30 This script creates a LyX document containing a table
31 from a comma-separated-value file. The LyX file has format 276
32 and can be opened with LyX 1.5.0 and newer.
33
34 Options:
35    -s separator    column separator, default is Tab
36    --help          usage instructions
37
38 Remarks:
39    If your .csv file contains special characters (e. g. umlauts,
40    accented letters, etc.) make sure it is coded in UTF-8 (unicode).
41    Else LyX will loose some cell contents.'''
42     sys.exit(0)
43
44 # print len(sys.argv), sys.argv
45 separator = '\t'
46 infile = ""
47 if len(sys.argv) == 3:
48         infile = sys.argv[1]
49         outfile = sys.argv[2]
50 elif len(sys.argv) == 5:
51         infile = sys.argv[3]
52         outfile = sys.argv[4]
53         if sys.argv[1] == '-s':
54                 separator = sys.argv[2]
55
56 if not os.path.exists(infile):
57         error('File "%s" not found.' % infile)
58 # read input
59 finput = open(infile, 'r')
60 rowcontent = finput.readlines()
61 finput.close()
62 num_rows = len(rowcontent) # number of lines
63 # print 'num_rows ', num_rows
64 i = 0
65 num_cols = 1 # max columns
66 while i < num_rows:
67         # print len(rowcontent[i]), '   ', rowcontent[i]
68         num_cols = max(num_cols, rowcontent[i].count(separator) + 1)
69         i += 1
70 # print num_cols
71
72 fout = open(outfile, 'w')
73 #####################
74 # write first part
75 ####################
76 fout.write("""#csv2lyx created this file
77 \lyxformat 276
78 \\begin_document
79 \\begin_header
80 \\textclass article
81 \\inputencoding auto
82 \\font_roman default
83 \\font_sans default
84 \\font_typewriter default
85 \\font_default_family default
86 \\font_sc false
87 \\font_osf false
88 \\font_sf_scale 100
89 \\font_tt_scale 100
90 \\graphics default
91 \\paperfontsize default
92 \\papersize default
93 \\use_geometry false
94 \\use_amsmath 1
95 \\use_esint 0
96 \\cite_engine basic
97 \\use_bibtopic false
98 \\paperorientation portrait
99 \\secnumdepth 3
100 \\tocdepth 3
101 \\paragraph_separation indent
102 \\defskip medskip
103 \\papercolumns 1
104 \\papersides 1
105 \\paperpagestyle default
106 \\tracking_changes false
107 \\output_changes false
108 \\end_header
109
110 \\begin_body
111 \\begin_layout Standard
112 \\align left 
113
114 \\begin_inset Tabular
115 \n""")
116 fout.write('<lyxtabular version="3" rows=' + str(num_rows) + ' columns=' + str(num_cols) + ' >\n')
117 fout.write('<features>\n')
118 #####################
119 # write table
120 ####################
121 i = 0
122 while i < num_cols:
123         fout.write('<column alignment="left" valignment="top" width="0pt">\n')
124         i += 1
125 j = 0
126 while j < num_rows:
127         fout.write('<row>\n')
128         row = str(rowcontent[j])
129         row = string.split(row,separator)
130         #print j, ': ' , row
131 ############################
132 # write contents of one line
133 ############################
134         i = 0
135         while i < num_cols:
136                 fout.write("""<cell alignment="left" valignment="top" usebox="none">
137 \\begin_inset Text
138 \\begin_layout Standard\n""")
139                 fout.write(row[i])
140                 fout.write('\\end_layout\n\\end_inset\n</cell>\n')
141                 i += 1
142         fout.write('</row>\n')
143         j += 1
144 #####################
145 # write last part
146 ####################
147 fout.write("""</lyxtabular>
148 \\end_inset
149 \\end_layout
150 \\end_body
151 \\end_document\n""")
152 fout.close()