|
1 | 1 | Extend pyexcel-io Tutorial |
2 | 2 | ================================================================================ |
3 | 3 |
|
4 | | -pyexcel-io itself comes with csv support. |
| 4 | +You are welcome extend pyexcel-io to read and write more tabular formats. In |
| 5 | +github repo, you will find two examples in `examples` folder. This section |
| 6 | +explains its implementations to help you write yours. |
| 7 | + |
| 8 | +.. note:: |
| 9 | + |
| 10 | + No longer, you will need to do explicit imports for pyexcel-io extensions. |
| 11 | + Instead, you install them and manage them via pip. |
5 | 12 |
|
6 | 13 | Reader |
7 | 14 | -------------------------------------------------------------------------------- |
8 | 15 |
|
9 | 16 | Suppose we have a yaml file, containing a dictionary where the values are |
10 | | -two dimensional array. The task is write reader plugin to pyexcel-io so that |
11 | | -we can use get_data() to read it out. |
12 | | - |
13 | | -Example yaml data:: |
| 17 | +two dimensional array. The task is to write a reader plugin to pyexcel-io so that |
| 18 | +we can use get_data() to read yaml file out. |
14 | 19 |
|
15 | 20 | .. literalinclude:: ../../examples/test.yaml |
16 | 21 | :language: yaml |
17 | | - |
18 | | -Example code:: |
19 | 22 |
|
20 | | -.. literalinclude:: ../../examples/custom_yeaml_reader.py |
| 23 | +**Implement IReader** |
| 24 | + |
| 25 | +First, let's impolement reader interface as below. Three implementations are required: |
| 26 | + |
| 27 | +1. `content_array` attribute, is expected to be a list of `NamedContent` |
| 28 | +2. `read_sheet` function, read sheet content by its index. |
| 29 | +3. `close` function, to clean up any file handle |
| 30 | + |
| 31 | +.. literalinclude:: ../../examples/custom_yaml_reader.py |
21 | 32 | :language: python |
| 33 | + :lines: 19-33 |
| 34 | + |
| 35 | +**Implement ISheet** |
22 | 36 |
|
| 37 | +`YourSingleSheet` makes this simple task complex in order to show case its inner |
| 38 | +workings. Two abstract functions require implementation: |
| 39 | + |
| 40 | +1. `row_iterator`: should return a row: either content arry or content index as long as |
| 41 | + `column_iterator` understands |
| 42 | + |
| 43 | +2. `column_iterator`: should return cell values one by one. |
| 44 | + |
| 45 | +.. literalinclude:: ../../examples/custom_yaml_reader.py |
| 46 | + :language: python |
| 47 | + :lines: 8-16 |
| 48 | + |
| 49 | + |
| 50 | +**Plug in pyexcel-io** |
| 51 | + |
| 52 | +Last thing is to register with pyexcel-io about your new reader. `relative_plugin_class_path` |
| 53 | +meant reference from current module, how to refer to `YourReader`. `locations` meant |
| 54 | +the physical presence of the data source: "file", "memory" or "content". "file" means |
| 55 | +files on physical disk. "memory" means a file stream. "content" means a string buffer. |
| 56 | +`stream_type` meant the type of the stream: binary for BytesIO and text for StringIO. |
| 57 | + |
| 58 | +.. literalinclude:: ../../examples/custom_yaml_reader.py |
| 59 | + :language: python |
| 60 | + :lines: 36-41 |
| 61 | + |
| 62 | + |
| 63 | +**Test your reader ** |
| 64 | +
|
| 65 | +Let's run the following code and see if it works. |
| 66 | + |
| 67 | +.. literalinclude:: ../../examples/custom_yaml_reader.py |
| 68 | + :language: python |
| 69 | + :lines: 43-45 |
23 | 70 |
|
24 | 71 | Writer |
25 | 72 | -------------------------------------------------------------------------------- |
26 | 73 |
|
| 74 | +Now for the writer, let's write a pyexcel-io writer that write a dictionary of |
| 75 | +two dimentaional arrays back into a yaml file seen above. |
27 | 76 |
|
| 77 | +** Implement IWriter ** |
28 | 78 |
|
29 | | -Working with xls, xlsx, and ods formats |
30 | | -================================================================================ |
| 79 | +Two abstract functions are required: |
31 | 80 |
|
32 | | -.. note:: |
| 81 | +1. `create_sheet` creates a native sheet by sheet name, that understands how to code up the native sheet. Interestingly, it returns your sheet. |
| 82 | +2. `close` function closes file handle if any. |
| 83 | + |
| 84 | +.. literalinclude:: ../../examples/custom_yaml_writer.py |
| 85 | + :language: python |
| 86 | + :lines: 18-30 |
| 87 | + |
| 88 | +** Implement ISheetWriter ** |
| 89 | + |
| 90 | +It is imagined that you will have your own sheet writer. You simply need to figure |
| 91 | +out how to write a row. Row by row write action was already written by `ISheetWrier`. |
| 92 | + |
| 93 | + |
| 94 | +.. literalinclude:: ../../examples/custom_yaml_writer.py |
| 95 | + :language: python |
| 96 | + :lines: 7-14 |
| 97 | + |
| 98 | +**Plug in pyexcel-io** |
| 99 | + |
| 100 | +Like the reader plugin, we register a writer. |
| 101 | + |
| 102 | +.. literalinclude:: ../../examples/custom_yaml_writer.py |
| 103 | + :language: python |
| 104 | + :lines: 33-38 |
| 105 | + |
| 106 | +**Test It** |
| 107 | + |
| 108 | +Let's run the following code and please examine `mytest.yaml` yourself. |
| 109 | + |
| 110 | +.. literalinclude:: ../../examples/custom_yaml_writer.py |
| 111 | + :language: python |
| 112 | + :lines: 40-46 |
33 | 113 |
|
34 | | - No longer, you will need to do explicit imports for pyexcel-io extensions. |
35 | | - Instead, you install them and manage them via pip. |
36 | 114 |
|
37 | | -Work with physical file |
| 115 | + |
| 116 | +Other pyexcel-io plugins |
38 | 117 | ----------------------------------------------------------------------------- |
39 | 118 |
|
| 119 | +Get xls support |
| 120 | + |
| 121 | +.. code-block:: |
| 122 | +
|
| 123 | +
|
| 124 | + $ pip install pyexcel-xls |
| 125 | +
|
| 126 | +
|
40 | 127 | Here's what is needed:: |
41 | 128 |
|
42 | 129 | >>> from pyexcel_io import save_data |
@@ -71,7 +158,6 @@ And you can also get the data back:: |
71 | 158 |
|
72 | 159 | The same applies to :meth:`pyexcel_io.get_data`. |
73 | 160 |
|
74 | | - |
75 | 161 | Other formats |
76 | 162 | ----------------------------------------------------------------------------- |
77 | 163 |
|
|
0 commit comments