|
| 1 | +# -*- coding: utf8 -*- |
| 2 | +from __future__ import absolute_import, print_function |
| 3 | + |
| 4 | +__all__ = ['open_excel', 'Workbook'] |
| 5 | + |
| 6 | + |
1 | 7 | import os |
2 | 8 | import atexit |
3 | 9 |
|
|
9 | 15 |
|
10 | 16 | from larray.core.group import _translate_sheet_name |
11 | 17 | from larray.core.axis import Axis |
12 | | -from larray.core.array import LArray |
| 18 | +from larray.core.array import LArray, ndtest |
13 | 19 | from larray.inout.array import df_aslarray, from_lists |
| 20 | +from larray.util.misc import PY2 |
14 | 21 |
|
15 | 22 | string_types = (str,) |
16 | 23 |
|
@@ -59,7 +66,6 @@ def write_value(cls, value, options): |
59 | 66 | # TODO: replace overwrite_file by mode='r'|'w'|'a' the day xlwings will support a read-only mode |
60 | 67 | class Workbook(object): |
61 | 68 | def __init__(self, filepath=None, overwrite_file=False, visible=None, silent=None, app=None): |
62 | | - """See open_excel doc for parameters""" |
63 | 69 | global global_app |
64 | 70 |
|
65 | 71 | xw_wkb = None |
@@ -226,10 +232,8 @@ def save(self, path=None): |
226 | 232 | self.xw_wkb.save(path=path) |
227 | 233 |
|
228 | 234 | def close(self): |
229 | | - """ |
230 | | - Close the workbook in Excel. This will not quit the Excel instance, even if this was the last workbook of |
231 | | - that Excel instance. |
232 | | - """ |
| 235 | + # Close the workbook in Excel. |
| 236 | + # This will not quit the Excel instance, even if this was the last workbook of that Excel instance. |
233 | 237 | if self.filepath is not None and os.path.isfile(self.xw_wkb.fullname): |
234 | 238 | tmp_file = self.xw_wkb.fullname |
235 | 239 | self.xw_wkb.close() |
@@ -555,13 +559,92 @@ def load(self, header=True, convert_float=True, nb_index=None, index_col=None, f |
555 | 559 | else: |
556 | 560 | return LArray(list_data) |
557 | 561 |
|
558 | | - # XXX: remove this function? |
| 562 | + # XXX: deprecate this function? |
559 | 563 | def open_excel(filepath=None, overwrite_file=False, visible=None, silent=None, app=None): |
560 | 564 | return Workbook(filepath, overwrite_file, visible, silent, app) |
561 | 565 | else: |
| 566 | + class Workbook(object): |
| 567 | + def __init__(self, filepath=None, overwrite_file=False, visible=None, silent=None, app=None): |
| 568 | + raise Exception("Workbook class cannot be instanciated because xlwings is not installed") |
| 569 | + |
| 570 | + def sheet_names(self): |
| 571 | + raise Exception() |
| 572 | + |
| 573 | + def save(self, path=None): |
| 574 | + raise Exception() |
| 575 | + |
| 576 | + def close(self): |
| 577 | + raise Exception() |
| 578 | + |
562 | 579 | def open_excel(filepath=None, overwrite_file=False, visible=None, silent=None, app=None): |
563 | 580 | raise Exception("open_excel() is not available because xlwings is not installed") |
564 | 581 |
|
| 582 | + |
| 583 | +# We define Workbook and open_excel documentation here since Readthedocs runs on Linux |
| 584 | +if not PY2: |
| 585 | + Workbook.__doc__ = """ |
| 586 | +Excel Workbook. |
| 587 | +
|
| 588 | +See Also |
| 589 | +-------- |
| 590 | +open_excel |
| 591 | +""" |
| 592 | + |
| 593 | + Workbook.sheet_names.__doc__ = """ |
| 594 | +Returns the names of the Excel sheets. |
| 595 | +
|
| 596 | +Examples |
| 597 | +-------- |
| 598 | +>>> arr, arr2, arr3 = ndtest((3, 3)), ndtest((2, 2)), ndtest(4) |
| 599 | +>>> with open_excel('excel_file.xlsx', overwrite_file=True) as wb: # doctest: +SKIP |
| 600 | +... wb['arr'] = arr.dump() |
| 601 | +... wb['arr2'] = arr2.dump() |
| 602 | +... wb['arr3'] = arr3.dump() |
| 603 | +... wb.save() |
| 604 | +... |
| 605 | +... wb.sheet_names() |
| 606 | +['arr', 'arr2', 'arr3'] |
| 607 | +""" |
| 608 | + |
| 609 | + Workbook.save.__doc__ = """ |
| 610 | +Saves the Workbook. |
| 611 | +
|
| 612 | +If a path is being provided, this works like SaveAs() in Excel. |
| 613 | +If no path is specified and if the file hasn’t been saved previously, |
| 614 | +it’s being saved in the current working directory with the current filename. |
| 615 | +Existing files are overwritten without prompting. |
| 616 | +
|
| 617 | +Parameters |
| 618 | +---------- |
| 619 | +path : str, optional |
| 620 | + Full path to the workbook. Defaults to None. |
| 621 | + |
| 622 | +Examples |
| 623 | +-------- |
| 624 | +>>> arr, arr2, arr3 = ndtest((3, 3)), ndtest((2, 2)), ndtest(4) |
| 625 | +>>> with open_excel('excel_file.xlsx', overwrite_file=True) as wb: # doctest: +SKIP |
| 626 | +... wb['arr'] = arr.dump() |
| 627 | +... wb['arr2'] = arr2.dump() |
| 628 | +... wb['arr3'] = arr3.dump() |
| 629 | +... wb.save() |
| 630 | +""" |
| 631 | + |
| 632 | + Workbook.close.__doc__ = """ |
| 633 | +Close the workbook in Excel. |
| 634 | +
|
| 635 | +Need to be called if the workbook has been opened without the `with` statement. |
| 636 | +
|
| 637 | +Examples |
| 638 | +-------- |
| 639 | +>>> arr, arr2, arr3 = ndtest((3, 3)), ndtest((2, 2)), ndtest(4) # doctest: +SKIP |
| 640 | +>>> wb = open_excel('excel_file.xlsx', overwrite_file=True) # doctest: +SKIP |
| 641 | +>>> wb['arr'] = arr.dump() # doctest: +SKIP |
| 642 | +>>> wb['arr2'] = arr2.dump() # doctest: +SKIP |
| 643 | +>>> wb['arr3'] = arr3.dump() # doctest: +SKIP |
| 644 | +>>> wb.save() # doctest: +SKIP |
| 645 | +>>> wb.close() # doctest: +SKIP |
| 646 | +""" |
| 647 | + |
565 | 648 | open_excel.__doc__ = """ |
566 | 649 | Open an Excel workbook |
567 | 650 |
|
@@ -592,7 +675,6 @@ def open_excel(filepath=None, overwrite_file=False, visible=None, silent=None, a |
592 | 675 |
|
593 | 676 | Examples |
594 | 677 | -------- |
595 | | ->>> from larray import * |
596 | 678 | >>> arr = ndtest((3, 3)) |
597 | 679 | >>> arr |
598 | 680 | a\\b b0 b1 b2 |
|
0 commit comments