[Python-modules-commits] [python-odf] 98/118: Contribution from Kartikaya Gupta
Wolfgang Borgert
debacle at moszumanska.debian.org
Fri Oct 3 21:27:28 UTC 2014
This is an automated email from the git hooks/post-receive script.
debacle pushed a commit to reference refs/remotes/upstream/master
in repository python-odf.
commit 7f01b5f4d32ae19bf6da61779ce95169a5a19892
Author: Søren Roug <soren.roug at eea.europa.eu>
Date: Mon Dec 27 09:39:44 2010 +0000
Contribution from Kartikaya Gupta
---
contrib/odscell/Makefile | 14 ++++
contrib/odscell/odscell | 184 ++++++++++++++++++++++++++++++++++++++++++++
contrib/odscell/odscell.1 | 98 +++++++++++++++++++++++
contrib/odscell/odscell.xml | 117 ++++++++++++++++++++++++++++
4 files changed, 413 insertions(+)
diff --git a/contrib/odscell/Makefile b/contrib/odscell/Makefile
new file mode 100644
index 0000000..da70bd8
--- /dev/null
+++ b/contrib/odscell/Makefile
@@ -0,0 +1,14 @@
+all: odf odscell.1
+
+txt: odscell.txt
+
+%.1: %.xml
+ xmlto man $<
+
+%.txt: %.xml
+ xmlto txt $<
+
+clean:
+ rm -f *.txt odf
+odf:
+ ln -s ../../odf
diff --git a/contrib/odscell/odscell b/contrib/odscell/odscell
new file mode 100755
index 0000000..316ad30
--- /dev/null
+++ b/contrib/odscell/odscell
@@ -0,0 +1,184 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+# Copyright (C) 2010 Kartikaya Gupta, https://staktrace.com/
+#
+# This is free software. You may redistribute it under the terms
+# of the Apache license and the GNU General Public License Version
+# 2 or at your option any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public
+# License along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+#
+# Contributor(s):
+#
+
+from odf import opendocument
+from odf.element import Text
+from odf.table import *
+from odf.text import P
+from optparse import OptionParser
+import sys,csv,re
+
+def getSheet( file, sheetIndex ):
+ doc = opendocument.load( file )
+ try:
+ spreadsheet = doc.spreadsheet
+ except NameError:
+ sys.stderr.write("Error: file is not a spreadsheet\n")
+ return (None, None)
+
+ sheets = spreadsheet.getElementsByType( Table )
+ if sheetIndex > len( sheets ):
+ sys.stderr.write( "Error: spreadsheet has only %d sheets; requested invalid sheet %d\n" % (len( sheets ), sheetIndex + 1) )
+ return (None, None)
+
+ sheet = sheets[sheetIndex]
+ return (doc, sheet)
+
+def displayCells( file, sheetIndex, rowIndex, colIndex, rowCount, colCount ):
+ (doc, sheet) = getSheet( file, sheetIndex )
+ if (doc == None or sheet == None):
+ return 1
+
+ rows = sheet.getElementsByType( TableRow )
+ if rowIndex + rowCount > len( rows ):
+ sys.stderr.write( "Error: sheet has only %d rows; requested invalid row %d\n" % (len( rows ), rowIndex + rowCount) )
+ return 1
+
+ csv_writer = csv.writer( sys.stdout )
+ for i in range( rowIndex, rowIndex + rowCount ):
+ row = rows[ i ]
+ cells = row.getElementsByType( TableCell )
+ if colIndex + colCount > len( cells ):
+ sys.stderr.write( "Error: row has only %d cells; requested invalid column %d\n" % (len( cells ), colIndex + colCount) )
+ return 1
+ for j in range( colIndex, colIndex + colCount ):
+ cells[ j ] = unicode( cells[ j ] )
+ csv_writer.writerow( cells[ colIndex : colIndex + colCount ] )
+ return 0
+
+def updateCells( file, sheetIndex, rowIndex, colIndex, rowCount, colCount ):
+ (doc, sheet) = getSheet( file, sheetIndex )
+ if (doc == None or sheet == None):
+ return 1
+
+ data = sys.stdin.readlines()
+ if rowCount < 0:
+ rowCount = len( data )
+ elif len( data ) < rowCount:
+ sys.stderr.write( "Error: not enough rows in input\n" )
+ return 1
+
+ # add table rows as necessary
+ rows = sheet.getElementsByType( TableRow )
+ if rowIndex + rowCount > len( rows ):
+ for i in range( rowIndex + rowCount - len( rows ) ):
+ sheet.addElement( TableRow() )
+ rows = sheet.getElementsByType( TableRow )
+
+ csv_reader = csv.reader( data )
+ for i in range( rowIndex, rowIndex + rowCount ):
+ row = rows[ i ]
+ dataRow = csv_reader.next()
+ rowColCount = colCount
+ if rowColCount < 0:
+ rowColCount = len( dataRow )
+ elif len( dataRow ) < rowColCount:
+ sys.stderr.write( "Error: not enough columns in input on row %d\n" % (rowIndex - i + 1) )
+ return 1
+
+ cells = row.getElementsByType( TableCell )
+ if colIndex > len( cells ):
+ for j in range( len( cells ), colIndex ):
+ row.addElement( TableCell() )
+ cells = row.getElementsByType( TableCell )
+
+ for j in range( colIndex, colIndex + rowColCount ):
+ if j < len( cells ):
+ row.insertBefore( TableCell(), cells[ j ].nextSibling );
+ row.removeChild( cells[ j ] )
+ else:
+ row.addElement( TableCell() )
+
+ cells = row.getElementsByType( TableCell )
+ value = dataRow[ j - colIndex ]
+ if re.match( r'^[-]?\d+(\.\d+)?$', value.strip() ):
+ cells[ j ].setAttribute( 'valuetype', 'float' )
+ cells[ j ].setAttribute( 'value', value )
+ else:
+ cells[ j ].setAttribute( 'valuetype', 'string' )
+ cells[ j ].addElement( P( text = dataRow[ j - colIndex ] ) )
+
+ doc.save( file )
+ return 0
+
+def parseCell( cell ):
+ cellmatch = re.match( r'^([A-Z]+)([0-9]+)$', cell )
+ if cellmatch == None:
+ sys.stderr.write( "Error: the cell specified was not in the required format of <column><row> (e.g. A1)" )
+ exit( 1 )
+
+ cellcol = cellmatch.group( 1 )
+ colIndex = 0
+ for i in range( len( cellcol ) ):
+ colIndex = (colIndex * 26) + (ord( cellcol[i] ) - ord( 'A' ) + 1)
+ colIndex = colIndex - 1
+
+ rowIndex = int( cellmatch.group( 2 ) ) - 1
+
+ return (colIndex, rowIndex)
+
+def getCount( value, write, label ):
+ if value == None:
+ if write:
+ return -1
+ else:
+ return 1
+
+ value = int( value )
+ if value <= 0:
+ sys.stderr.write( "Error: illegal value specified for %s\n" % label )
+ exit( 1 )
+ return value
+
+if __name__ == "__main__":
+ usage = "%prog file.ods cell"
+ parser = OptionParser( usage=usage, version="%prog 0.1" )
+ parser.add_option( '-r', '--rows', action='store', dest='rows', help=
+'''Specify the height of the block of cells, in rows. Must be greater than zero. Defaults to 1 when
+the -w option is not present. Defaults to the number of input rows when the -w option is present.''', default=None )
+ parser.add_option( '-c', '--cols', action='store', dest='cols', help=
+'''Specify the width of the block of cells, in columns. Must be greater than zero. Defaults to 1 when
+the -w option is not present. Defaults to the number of input columns when the -w option is present.''', default=None )
+ parser.add_option( '-s', '--sheet', action='store', dest='sheet', help='The sheet in the ODS file to read/modify. Must be greater than zero; defaults to 1.', default=1 )
+ parser.add_option( '-w', '--write', action='store_true', dest='write', help=
+'''If specified, the spreadsheet will be modified with data from standard input. If not specified,
+the cells from the spreadsheet will be written to standard output.''' )
+
+ (options, args) = parser.parse_args()
+
+ if len( args ) != 2:
+ parser.print_help()
+ exit( 1 )
+
+ file = args[0]
+ (colIndex, rowIndex) = parseCell( args[1] )
+
+ rowCount = getCount( options.rows, options.write, 'rows' )
+ colCount = getCount( options.cols, options.write, 'cols' )
+ sheet = int( options.sheet ) - 1
+
+ if sheet < 0:
+ sys.stderr.write( "Error: illegal value specified for sheet\n" )
+ exit( 1 )
+
+ if options.write:
+ exit( updateCells( file, sheet, rowIndex, colIndex, rowCount, colCount ) )
+ else:
+ exit( displayCells( file, sheet, rowIndex, colIndex, rowCount, colCount ) )
diff --git a/contrib/odscell/odscell.1 b/contrib/odscell/odscell.1
new file mode 100644
index 0000000..25f5e6f
--- /dev/null
+++ b/contrib/odscell/odscell.1
@@ -0,0 +1,98 @@
+'\" t
+.\" Title: odscell
+.\" Author: Kartikaya Gupta
+.\" Generator: DocBook XSL Stylesheets v1.76.0 <http://docbook.sf.net/>
+.\" Date: 12/22/2010
+.\" Manual: User commands
+.\" Source: odfpy
+.\" Language: English
+.\"
+.TH "ODSCELL" "1" "12/22/2010" "odfpy" "User commands"
+.\" -----------------------------------------------------------------
+.\" * Define some portability stuff
+.\" -----------------------------------------------------------------
+.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+.\" http://bugs.debian.org/507673
+.\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
+.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+.ie \n(.g .ds Aq \(aq
+.el .ds Aq '
+.\" -----------------------------------------------------------------
+.\" * set default formatting
+.\" -----------------------------------------------------------------
+.\" disable hyphenation
+.nh
+.\" disable justification (adjust text to left margin only)
+.ad l
+.\" -----------------------------------------------------------------
+.\" * MAIN CONTENT STARTS HERE *
+.\" -----------------------------------------------------------------
+.SH "NAME"
+odscell \- Read and update blocks of cells in OpenDocument spreadsheets files
+.SH "SYNOPSIS"
+.HP \w'\fBodscell\fR\ 'u
+\fBodscell\fR \fIfile\&.ods\fR \fIcell\fR
+.SH "DESCRIPTION"
+.PP
+This program reads a cell or block of cells from a file in ODS format, and prints it out in a CSV format to standard output\&. Alternatively, if the \-w flag is set, the program reads in a CSV\-formatted block of cells from standard input, and overwites a cell or block of cells in a file in ODS format\&.
+.SH "OPTIONS"
+.PP
+\-\-version
+.RS 4
+Display the version and exit\&.
+.RE
+.PP
+\-h, \-\-help
+.RS 4
+Display command usage\&.
+.RE
+.PP
+\-r \fIROWS\fR, \-\-rows=\fIROWS\fR
+.RS 4
+Specify the height of the block of cells, in rows\&. Must be greater than zero\&. Defaults to 1 when the \-w option is not present\&. Defaults to the number of input rows when the \-w option is present\&.
+.RE
+.PP
+\-c \fICOLS\fR, \-\-cols=\fICOLS\fR
+.RS 4
+Specify the width of the block of cells, in columns\&. Must be greater than zero\&. Defaults to 1 when the \-w option is not present\&. Defaults to the number of input columns when the \-w option is present\&.
+.RE
+.PP
+\-s \fISHEET\fR, \-\-sheet=\fISHEET\fR
+.RS 4
+The sheet in the ODS file to read/modify\&. Must be greater than zero; defaults to 1\&.
+.RE
+.PP
+\-w
+.RS 4
+If specified, the spreadsheet will be modified with data from standard input\&. If not specified, the cells from the spreadsheet will be written to standard output\&.
+.RE
+.PP
+\fIfile\&.ods\fR
+.RS 4
+The ODS file to be read from or modified\&.
+.RE
+.PP
+\fIcell\fR
+.RS 4
+The top\-left cell of the block of cells to be read from or modified\&. This should be specified in normal spreadsheet format, e\&.g\&. "A1" or "BA23"\&.
+.RE
+.SH "EXAMPLE"
+.sp
+.if n \{\
+.RS 4
+.\}
+.nf
+odscell foo\&.ods A4 # display value in cell A4 on sheet 1 of foo\&.ods
+odscell \-r 2 \-c 2 foo\&.ods B2 # display values for cells B2,B3,C2,C3 on sheet 1 of foo\&.ods
+echo "hello,world,garbage" | odscell \-c 2 \-w foo\&.ods A1 # write "hello" to cell A1 and "world" to cell A2 on sheet 1 of foo\&.ods
+cat bar\&.csv | odscell \-s 2 \-w foo\&.ods A1 # put the CSV data from bar\&.csv into sheet 2 of foo\&.ods
+.fi
+.if n \{\
+.RE
+.\}
+.SH "AUTHOR"
+.PP
+\fBKartikaya Gupta\fR
+.RS 4
+Original author of odscell
+.RE
diff --git a/contrib/odscell/odscell.xml b/contrib/odscell/odscell.xml
new file mode 100644
index 0000000..982b933
--- /dev/null
+++ b/contrib/odscell/odscell.xml
@@ -0,0 +1,117 @@
+<?xml version="1.0"?>
+<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN" "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
+<refentry id="odscell">
+ <refentryinfo>
+ <productname>odfpy</productname>
+ <author><firstname>Kartikaya</firstname><surname>Gupta</surname>
+ <contrib>Original author of odscell</contrib>
+ </author>
+ </refentryinfo>
+ <refmeta>
+ <refentrytitle>odscell</refentrytitle>
+ <manvolnum>1</manvolnum>
+ <refmiscinfo class="manual">User commands</refmiscinfo>
+ </refmeta>
+ <refnamediv>
+ <refname>odscell</refname>
+ <refpurpose>Read and update blocks of cells in OpenDocument spreadsheets files</refpurpose>
+ </refnamediv>
+ <refsynopsisdiv>
+ <cmdsynopsis>
+ <command>odscell</command>
+ <arg choice="plain"><replaceable>file.ods</replaceable></arg>
+ <arg choice="plain"><replaceable>cell</replaceable></arg>
+ </cmdsynopsis>
+ </refsynopsisdiv>
+ <refsect1>
+ <title>Description</title>
+ <para>
+This program reads a cell or block of cells from a file in ODS format, and prints it out
+in a CSV format to standard output. Alternatively, if the -w flag is set, the program reads
+in a CSV-formatted block of cells from standard input, and overwites a cell or block of cells
+in a file in ODS format.
+</para>
+ </refsect1>
+ <refsect1>
+ <title>Options</title>
+ <variablelist>
+ <varlistentry>
+ <term>--version</term>
+ <listitem>
+ <para>
+Display the version and exit.
+</para>
+ </listitem>
+ </varlistentry>
+ <varlistentry>
+ <term>-h, --help</term>
+ <listitem>
+ <para>
+Display command usage.
+</para>
+ </listitem>
+ </varlistentry>
+ <varlistentry>
+ <term>-r <replaceable>ROWS</replaceable>, --rows=<replaceable>ROWS</replaceable></term>
+ <listitem>
+ <para>
+Specify the height of the block of cells, in rows. Must be greater than zero. Defaults to 1 when
+the -w option is not present. Defaults to the number of input rows when the -w option is present.
+</para>
+ </listitem>
+ </varlistentry>
+ <varlistentry>
+ <term>-c <replaceable>COLS</replaceable>, --cols=<replaceable>COLS</replaceable></term>
+ <listitem>
+ <para>
+Specify the width of the block of cells, in columns. Must be greater than zero. Defaults to 1 when
+the -w option is not present. Defaults to the number of input columns when the -w option is present.
+</para>
+ </listitem>
+ </varlistentry>
+ <varlistentry>
+ <term>-s <replaceable>SHEET</replaceable>, --sheet=<replaceable>SHEET</replaceable></term>
+ <listitem>
+ <para>
+The sheet in the ODS file to read/modify. Must be greater than zero; defaults to 1.
+</para>
+ </listitem>
+ </varlistentry>
+ <varlistentry>
+ <term>-w</term>
+ <listitem>
+ <para>
+If specified, the spreadsheet will be modified with data from standard input. If not specified,
+the cells from the spreadsheet will be written to standard output.
+</para>
+ </listitem>
+ </varlistentry>
+ <varlistentry>
+ <term><replaceable>file.ods</replaceable></term>
+ <listitem>
+ <para>
+The ODS file to be read from or modified.
+</para>
+ </listitem>
+ </varlistentry>
+ <varlistentry>
+ <term><replaceable>cell</replaceable></term>
+ <listitem>
+ <para>
+The top-left cell of the block of cells to be read from or modified. This should be specified
+in normal spreadsheet format, e.g. "A1" or "BA23".
+</para>
+ </listitem>
+ </varlistentry>
+ </variablelist>
+ </refsect1>
+ <refsect1>
+ <title>Example</title>
+ <screen>
+odscell foo.ods A4 # display value in cell A4 on sheet 1 of foo.ods
+odscell -r 2 -c 2 foo.ods B2 # display values for cells B2,B3,C2,C3 on sheet 1 of foo.ods
+echo "hello,world,garbage" | odscell -c 2 -w foo.ods A1 # write "hello" to cell A1 and "world" to cell A2 on sheet 1 of foo.ods
+cat bar.csv | odscell -s 2 -w foo.ods A1 # put the CSV data from bar.csv into sheet 2 of foo.ods
+</screen>
+ </refsect1>
+</refentry>
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/python-modules/packages/python-odf.git
More information about the Python-modules-commits
mailing list