Skip to main content

Write Spreadsheet

Command description

Writes data to a cell, row, or column using append, insert, or overwrite mode.

Command Input Parameters

input parametersInput parameter typeDescription
Excel ObjectExcelAppSpreadsheet object to write to
Write RangeenumRow, Column, Cell
Row NumberstrOne-based starting row; -n means the nth row from the bottom
Column NamestrStarting column letter such as A or one-based column number; -n means the nth column from the right
Write ModeenumAppend, Insert, Overwrite. Required when Write Range is Row or Column
Content to Writelist/list[list]/anyContent to write. Two-dimensional lists are written row-first; for example, [[1, 2], [3, 4]] fills a two-row, two-column range
sheetstrWorksheet name. Defaults to the active worksheet

Command Output Parameters

None

Description

  • Row Number and Column Name:

    • When writing a row, Column Name specifies the first column to write.
    • When writing a column, Row Number specifies the first row to write.
  • Write Mode:

    • Append: Add a new row after the last row, or a new column after the last column.
    • Insert: Insert a row or column at the specified position and shift existing data accordingly.
    • Overwrite: Replace data in the specified row, column, or cell.
  • Content to Write:

    • Normal values are written to a single cell.
    • A one-dimensional list such as ['a', 'b', 'c'] is written as one row or one column, according to Write Range.
    • A two-dimensional list is written row-first into a rectangular range. For example, [[1, 2], [3, 4]] writes two rows and two columns starting at the specified cell.
    • If its rows have different lengths, shorter rows are padded with empty strings.
    • Cells to the left of the starting column or above the starting row are not modified.
tip

The default openpyxl mode does not require the installation of Microsoft Office or Kingsoft WPS. If the target file is being opened by Office/WPS, saving may fail. Please close the window occupying the file and try again.

Example

  • raw data
ABCD
1AppleBananaTomato
2CarTruckTaxi
3BallLegoDoll
  • Writing range: Row, column name: C, writing method: Append, writing content ['a','b','c','d']
ABCDEF
1AppleBananaTomato
2CarTruckTaxi
3BallLegoDoll
4abcd
  • Writing range: Row, column name: C, writing method: Insert, row number: 2, writing content ['a','b','c','d']
ABCDEF
1AppleBananaTomato
2abcd
3CarTruckTaxi
4BallLegoDoll
5
  • Writing range: Row, column name: C, writing method: Overwrite, row number: 2, writing content ['a','b','c','d']
ABCDEF
1AppleBananaTomato
2Carabcd
3BallLegoDoll
4
  • Writing range: Column, row number: 2, writing method: Append, writing content ['a','b','c','d']
ABCDEF
1AppleBananaTomato
2CarTruckTaxia
3BallLegoDollb
4c
5d
  • Writing range: Column, row number: 2, writing method: Insert, column name C, writing content ['a','b','c','d']
ABCDEF
1AppleBananaTomato
2CaraTruckTaxi
3BallbLegoDoll
4c
5d
  • Writing range: Column, row number: 2, writing method: Overwrite, column name C, writing content ['a','b','c','d']
ABCDEF
1AppleBananaTomato
2CaraTaxi
3BallbDoll
4c
5d
  • Writing range: Cell, row number: 2, column name C, writing content 'a'
ABCDEF
1AppleBananaTomato
2CaraTaxi
3BallLegoDoll
4
  • Writing range: Cell, row number: 3, column name D, writing content [[1, 2], [3, 4]]
ABCDEF
1AppleBananaTomato
2CarTruckTaxi
3BallLego12
434

This example will only modify the D3:E4 area, and the A:C column will not be modified.

Reference

Read Spreadsheet Displayed Data