From 3fd81c73cfd8bad36b2a1cf7955006e35c1d9db5 Mon Sep 17 00:00:00 2001 From: Camil Staps Date: Fri, 23 Oct 2015 16:44:24 +0200 Subject: Assignment 3: code, plots --- Assignment 3/packages/xlrd/examples/namesdemo.xls | Bin 0 -> 22528 bytes .../packages/xlrd/examples/xlrdnameAPIdemo.py | 179 +++++++++++++++++++++ 2 files changed, 179 insertions(+) create mode 100644 Assignment 3/packages/xlrd/examples/namesdemo.xls create mode 100644 Assignment 3/packages/xlrd/examples/xlrdnameAPIdemo.py (limited to 'Assignment 3/packages/xlrd/examples') diff --git a/Assignment 3/packages/xlrd/examples/namesdemo.xls b/Assignment 3/packages/xlrd/examples/namesdemo.xls new file mode 100644 index 0000000..8a16865 Binary files /dev/null and b/Assignment 3/packages/xlrd/examples/namesdemo.xls differ diff --git a/Assignment 3/packages/xlrd/examples/xlrdnameAPIdemo.py b/Assignment 3/packages/xlrd/examples/xlrdnameAPIdemo.py new file mode 100644 index 0000000..b29a827 --- /dev/null +++ b/Assignment 3/packages/xlrd/examples/xlrdnameAPIdemo.py @@ -0,0 +1,179 @@ +# -*- coding: cp1252 -*- + +## +# Module/script example of the xlrd API for extracting information +# about named references, named constants, etc. +# +#
Copyright © 2006 Stephen John Machin, Lingfo Pty Ltd
+#This module is part of the xlrd package, which is released under a BSD-style licence.
+## +from __future__ import print_function + +import xlrd +from xlrd.timemachine import REPR +import sys +import glob + +def scope_as_string(book, scope): + if 0 <= scope < book.nsheets: + return "sheet #%d (%r)" % (scope, REPR(book.sheet_names()[scope])) + if scope == -1: + return "Global" + if scope == -2: + return "Macro/VBA" + return "Unknown scope value (%r)" % REPR(scope) + +def do_scope_query(book, scope_strg, show_contents=0, f=sys.stdout): + try: + qscope = int(scope_strg) + except ValueError: + if scope_strg == "*": + qscope = None # means "all' + else: + # so assume it's a sheet name ... + qscope = book.sheet_names().index(scope_strg) + print("%r => %d" % (scope_strg, qscope), file=f) + for nobj in book.name_obj_list: + if qscope is None or nobj.scope == qscope: + show_name_object(book, nobj, show_contents, f) + +def show_name_details(book, name, show_contents=0, f=sys.stdout): + """ + book -- Book object obtained from xlrd.open_workbook(). + name -- The name that's being investigated. + show_contents -- 0: Don't; 1: Non-empty cells only; 2: All cells + f -- Open output file handle. + """ + name_lcase = name.lower() # Excel names are case-insensitive. + nobj_list = book.name_map.get(name_lcase) + if not nobj_list: + print("%r: unknown name" % name, file=f) + return + for nobj in nobj_list: + show_name_object(book, nobj, show_contents, f) + +def show_name_details_in_scope( + book, name, scope_strg, show_contents=0, f=sys.stdout, + ): + try: + scope = int(scope_strg) + except ValueError: + # so assume it's a sheet name ... + scope = book.sheet_names().index(scope_strg) + print("%r => %d" % (scope_strg, scope), file=f) + name_lcase = name.lower() # Excel names are case-insensitive. + while 1: + nobj = book.name_and_scope_map.get((name_lcase, scope)) + if nobj: + break + print("Name %s not found in scope %d" % (REPR(name), scope), file=f) + if scope == -1: + return + scope = -1 # Try again with global scope + print("Name %s found in scope %d" % (REPR(name), scope), file=f) + show_name_object(book, nobj, show_contents, f) + +def showable_cell_value(celltype, cellvalue, datemode): + if celltype == xlrd.XL_CELL_DATE: + try: + showval = xlrd.xldate_as_tuple(cellvalue, datemode) + except xlrd.XLDateError as e: + showval = "%s:%s" % (type(e).__name__, e) + elif celltype == xlrd.XL_CELL_ERROR: + showval = xlrd.error_text_from_code.get( + cellvalue, '