|
| 1 | +# Streaming write |
| 2 | + |
| 3 | +## Get stream writer {#NewStreamWriter} |
| 4 | + |
| 5 | +```python |
| 6 | +def new_stream_writer(sheet: str) -> StreamWriter |
| 7 | +``` |
| 8 | + |
| 9 | +NewStreamWriter returns stream writer struct by given worksheet name used for writing data on a new existing empty worksheet with large amounts of data. Note that after writing data with the stream writer for the worksheet, you must call the [`flush`](stream.md#Flush) method to end the streaming writing process, ensure that the order of row numbers is ascending when set rows, and the normal mode functions and stream mode functions can not be work mixed to writing data on the worksheets. The stream writer will try to use temporary files on disk to reduce the memory usage when in-memory chunks data over 16MB, and you can't get cell value at this time. For example, set data for worksheet of size `102400` rows x `50` columns with numbers and style: |
| 10 | + |
| 11 | +```python |
| 12 | +import excelize, random |
| 13 | + |
| 14 | +f = excelize.new_file() |
| 15 | +try: |
| 16 | + sw = f.new_stream_writer("Sheet1") |
| 17 | + for r in range(2, 102401): |
| 18 | + row = [random.randrange(640000) for _ in range(1, 51)] |
| 19 | + cell = excelize.coordinates_to_cell_name(1, r, False) |
| 20 | + sw.set_row(cell, row) |
| 21 | + sw.flush() |
| 22 | + f.save_as("Book1.xlsx") |
| 23 | +except RuntimeError as err: |
| 24 | + print(err) |
| 25 | +finally: |
| 26 | + err = f.close() |
| 27 | + if err: |
| 28 | + print(err) |
| 29 | +``` |
| 30 | + |
| 31 | +## Write sheet row in stream {#SetRow} |
| 32 | + |
| 33 | +```python |
| 34 | +def set_row( |
| 35 | + cell: str, |
| 36 | + values: List[Union[None, int, str, bool, datetime, date]], |
| 37 | +) -> None |
| 38 | +``` |
| 39 | + |
| 40 | +Writes an array to stream rows by giving starting cell reference and a pointer to an array of values. Note that you must call the [`flush`](stream.md#Flush) function to end the streaming writing process. |
| 41 | + |
| 42 | +## Add table in stream {#AddTable} |
| 43 | + |
| 44 | +```python |
| 45 | +def add_table(table: Table) -> None |
| 46 | +``` |
| 47 | + |
| 48 | +Creates an Excel table for the stream writer using the given cell range and format set. |
| 49 | + |
| 50 | +Note that the table must be at least two lines including the header. The header cells must contain strings and must be unique. Currently, only one table is allowed for a stream writer. The function must be called after the rows are written but before `flush`. |
| 51 | + |
| 52 | +Example 1, create a table of `A1:D5`: |
| 53 | + |
| 54 | +```python |
| 55 | +try: |
| 56 | + sw.add_table(excelize.Table(range="A1:D5")) |
| 57 | +except RuntimeError as err: |
| 58 | + print(err) |
| 59 | +``` |
| 60 | + |
| 61 | +Example 2, create a table of `F2:H6` with format set: |
| 62 | + |
| 63 | +```python |
| 64 | +try: |
| 65 | + sw.add_table( |
| 66 | + excelize.Table( |
| 67 | + range="F2:H6, |
| 68 | + name="table", |
| 69 | + style_name="TableStyleMedium2", |
| 70 | + show_first_column=True, |
| 71 | + show_last_column=True, |
| 72 | + show_row_stripes=False, |
| 73 | + show_column_stripes=True, |
| 74 | + ) |
| 75 | + ) |
| 76 | +except RuntimeError as err: |
| 77 | + print(err) |
| 78 | +``` |
| 79 | + |
| 80 | +Note that the table must be at least two lines including the header. The header cells must contain strings and must be unique. Currently only one table is allowed for a `StreamWriter`. [`add_table`](stream.md#AddTable) must be called after the rows are written but before `flush`. See [`add_able`](utils.md#AddTable) for details on the table format. |
| 81 | + |
| 82 | +## Insert page break in stream {#InsertPageBreak} |
| 83 | + |
| 84 | +```python |
| 85 | +def insert_page_break(cell: str) -> None |
| 86 | +``` |
| 87 | + |
| 88 | +Creates a page break to determine where the printed page ends and where begins the next one by a given cell reference, the content before the page break will be printed on one page and after the page break on another. |
| 89 | + |
| 90 | +## Set panes in stream {#SetPanes} |
| 91 | + |
| 92 | +```python |
| 93 | +def set_panes(opts: Panes) -> None |
| 94 | +``` |
| 95 | + |
| 96 | +Create and remove freeze panes and split panes by giving panes options for the `StreamWriter`. Note that you must call the `set_panes` function before the [`set_row`](stream.md#SetRow) function. |
| 97 | + |
| 98 | +## Merge cell in stream {#MergeCell} |
| 99 | + |
| 100 | +```python |
| 101 | +def merge_cell(top_left_cell: str, bottom_right_cell: str) -> None |
| 102 | +``` |
| 103 | + |
| 104 | +Merge cells by a given range reference for the `StreamWriter`. Don't create a merged cell that overlaps with another existing merged cell. |
| 105 | + |
| 106 | +## Set column width in stream {#SetColWidth} |
| 107 | + |
| 108 | +```python |
| 109 | +def set_col_width(start_col: int, end_col: int, width: float) -> None |
| 110 | +``` |
| 111 | + |
| 112 | +Set the width of a single column or multiple columns for the `StreamWriter`. Note that you must call the `set_col_width` function before the [`set_row`](stream.md#SetRow) function. For example set the width column `B:C` as `20`: |
| 113 | + |
| 114 | +```python |
| 115 | +try: |
| 116 | + sw.set_col_width(2, 3, 20) |
| 117 | +except RuntimeError as err: |
| 118 | + print(err) |
| 119 | +``` |
| 120 | + |
| 121 | +## Flush stream {#Flush} |
| 122 | + |
| 123 | +```python |
| 124 | +def flush() -> None |
| 125 | +``` |
| 126 | + |
| 127 | +Ending the streaming writing process. |
0 commit comments