Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #91531

Re: Python write to spreadsheet?

Path csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python.list@tim.thechases.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'subject:Python': 0.05; 'automate': 0.07; 'column': 0.07; 'data:': 0.07; 'filename': 0.07; 'repeated': 0.07; 'sys,': 0.07; 'sys.platform': 0.07; 'api': 0.09; 'item,': 0.09; 'parsers': 0.09; 'cc:addr:python-list': 0.10; 'python': 0.11; 'def': 0.14; 'file,': 0.15; '"win32":': 0.16; '-tkc': 0.16; 'csv': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'justin': 0.16; 'optionally': 0.16; 'reads,': 0.16; 'route,': 0.16; 'subprocess': 0.16; 'wrote:': 0.16; 'string': 0.17; 'windows': 0.20; 'cc:2**0': 0.21; 'cc:addr:python.org': 0.21; '(the': 0.22; 'latter': 0.22; 'os,': 0.22; 'cc:no real name:2**0': 0.23; 'module': 0.23; "python's": 0.23; 'specified': 0.23; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'cells': 0.29; 'excel': 0.29; 'launch': 0.29; 'common': 0.33; 'finally,': 0.33; 'machine.': 0.33; 'operations.': 0.33; 'surely': 0.33; 'open': 0.33; 'subject:?': 0.34; 'file': 0.34; 'add': 0.34; 'library.': 0.35; 'skip:o 20': 0.35; 'but': 0.36; 'there': 0.36; 'possible': 0.36; 'data.': 0.36; 'depends': 0.36; 'received:10': 0.37; 'subject:: ': 0.37; 'charset:us-ascii': 0.37; 'item': 0.38; 'files': 0.38; 'whatever': 0.39; 'data': 0.40; 'registered': 0.40; 'some': 0.40; 'your': 0.60; 'even': 0.61; 'received:46': 0.63; 'outline': 0.66; 'lack': 0.76; 'add-on': 0.84; 'excel,': 0.84; 'excel.': 0.84; 'spreadsheet': 0.84; 'subject:write': 0.84
X-Sender-Id wwwh|x-authuser|tim@thechases.com
X-Sender-Id wwwh|x-authuser|tim@thechases.com
X-MC-Relay Neutral
X-MailChannels-SenderId wwwh|x-authuser|tim@thechases.com
X-MailChannels-Auth-Id wwwh
X-MC-Loop-Signature 1432988637275:2980476354
X-MC-Ingress-Time 1432988637274
Date Sat, 30 May 2015 07:24:22 -0500
From Tim Chase <python.list@tim.thechases.com>
To Justin Thyme <JustinThyme@nowhere.com>
Cc python-list@python.org
Subject Re: Python write to spreadsheet?
In-Reply-To <mkbvuu$udo$1@news.albasani.net>
References <mkbvuu$udo$1@news.albasani.net>
X-Mailer Claws Mail 3.11.1 (GTK+ 2.24.25; x86_64-pc-linux-gnu)
MIME-Version 1.0
Content-Type text/plain; charset=US-ASCII
Content-Transfer-Encoding 7bit
X-AuthUser tim@thechases.com
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.226.1432990967.5151.python-list@python.org> (permalink)
Lines 53
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1432990967 news.xs4all.nl 2966 [2001:888:2000:d::a6]:37179
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:91531

Show key headers only | View raw


On 2015-05-30 10:30, Justin Thyme wrote:
> Is it possible to write a Python program that will start MS Excel, 
> create a spreadsheet and fill cells A1 to A10 (say) with the data
> in a Python array?  The answer is surely yes, but is there an
> outline of how to do it somewhere?

it depends on how strictly you want to keep your order of operations.

You *can* use Windows API calls to automate Excel, instructing it to
create a new sheet, then populate the various cells with data.
However, this is fragile, Windows-specific, and requires the target
to have Excel on their machine.

If you're willing to forego your specified ordering and create a
file, then open it in Excel, you can use the add-on xlrd/xlwt packages
(the former reads, the latter writes) to write XLS files that you can
then open in Excel.

Finally, if you're going down that route, the *easiest* way is to
simply use Python's csv module from the standard library.

  import csv
  FILENAME = "out.csv"
  with open(FILENAME, 'wb') as f:
    w = csv.writer(f)
    for item in data:
      w.writerow([
        item,
        #, ""  # optionally add a blank column because
               # some parsers like csv.Sniffer will notice
               # the lack of commas and try to find the most
               # common repeated string
        ])

You can then launch that file in Excel, or even better, whatever the
registered program is:

  import os, sys, subprocess
  def open_file(filename):
    if sys.platform == "win32":
      os.startfile(filename)
    else:
      if sys.platform == "darwin":
        program = "open"
      else:
        program = "xdg-open"
      subprocess.call([program, filename])
  open_file(FILENAME)

-tkc


Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Python write to spreadsheet? Justin Thyme <JustinThyme@nowhere.com> - 2015-05-30 10:30 +0100
  Re: Python write to spreadsheet? Tim Golden <mail@timgolden.me.uk> - 2015-05-30 10:40 +0100
  Re: Python write to spreadsheet? Joel Goldstick <joel.goldstick@gmail.com> - 2015-05-30 07:03 -0400
  Re: Python write to spreadsheet? Laura Creighton <lac@openend.se> - 2015-05-30 13:15 +0200
  Re: Python write to spreadsheet? Tim Chase <python.list@tim.thechases.com> - 2015-05-30 07:24 -0500
  Re: Python write to spreadsheet? Pablo Lucena <plucena24@gmail.com> - 2015-05-30 09:11 -0400

csiph-web