ans2dev.general.open_xl module – Read, Create and update Excel files.
Note
This module is part of the ans2dev.general collection (version 0.2.0).
It is not included in ansible-core
.
To check whether it is installed, run ansible-galaxy collection list
.
To install it, use: ansible-galaxy collection install ans2dev.general
.
You need further requirements to be able to use this module,
see Requirements for details.
To use it in a playbook, specify: ans2dev.general.open_xl
.
New in ans2dev.general 0.1.0
Synopsis
This module reads from or writes to Excel (.xlsx) files using the openpyxl Python library.
Create new Excel file is avaible from ans2dev.general 0.2.0.
It supports reading the entire workbook or a single worksheet, optionally limited to a given cell range.
For updates, you can overwrite cells, append new rows, or insert rows. You can also apply custom cell styles.
The original Excel file is not overwritten unless you set
dest
to the same path assrc
.This module supports only .xlsx or .xlsm files.
Requirements
The below requirements are needed on the host that executes this module.
openpyxl
Parameters
Parameter |
Comments |
---|---|
A dictionary specifying optional style attributes for updated cells. Possible keys include
Default: |
|
Destination file path for updated Excel content. If omitted, defaults to appending |
|
For read operations, if true, uses the first row as dictionary keys. Otherwise, keys are in the format Choices:
|
|
The operation to perform on the Excel file.
Choices:
|
|
Dictionary specifying the cell range to read. Can include If omitted or partially specified, defaults to the entire used range. Default: |
|
Path to the source Excel file. |
|
A list of dictionaries describing the cells to update.Each dictionary can include
Default: |
Notes
Note
This module requires the openpyxl Python library to be installed.
Only .xlsx or .xlsm files are supported.
Examples
- name: Read Excel workbook
ans2dev.general.open_xl:
src: "/tmp/sample.xlsx"
op: "r"
index_by_name: true
register: result
- debug:
var: result
- name: Overwrite specific cells
ans2dev.general.open_xl:
src: "/tmp/sample.xlsx"
dest: "/tmp/sample_updated.xlsx"
op: "w"
sheet_name: "Sheet1"
updates_matrix:
- cell_row: 2
cell_col: 1
cell_value: "New Value in row2 col1"
- cell_row: 3
cell_col: 2
cell_value: "Another Value"
cell_style:
fontColor: "FF0000"
bgColor: "FFFF00"
bold: true
- name: Append new row
ans2dev.general.open_xl:
src: "/tmp/sample.xlsx"
dest: "/tmp/sample_updated.xlsx"
op: "a"
sheet_name: "Sheet1"
updates_matrix:
- cell_col: 1
cell_value: "Hostname"
- cell_col: 2
cell_value: "MyHost"
cell_style:
bgColor: "DDEBF7"
bold: true
- name: Insert a new row above row 5
ans2dev.general.open_xl:
src: "/tmp/sample.xlsx"
dest: "/tmp/sample_updated.xlsx"
op: "i"
sheet_name: "Sheet1"
updates_matrix:
- cell_row: 5
cell_col: 1
cell_value: "Inserted"
- cell_row: 5
cell_col: 2
cell_value: "Row"
cell_style:
italic: true
# Create excel file from ans2dev.general 0.2.0
- name: Create a new Excel file and write data
ans2dev.general.open_xl:
dest: "/tmp/new_file.xlsx"
op: "n"
sheet_name: "Data"
updates_matrix:
- cell_row: 1
cell_col: 1
cell_value: "Header"
Return Values
Common return values are documented here, the following are the fields unique to this module:
Key |
Description |
---|---|
Indicates whether the Excel file was modified. For read operations, this is still returned as true, even though no changes were made to the file. Returned: always Sample: |
|
For read operations, returns a dictionary keyed by sheet name. Each key maps to a list of dictionaries, where each dictionary represents a row with cell values. The keys for each row are determined by the header row (if index_by_name is true) or default to “col_<n>”. For write, append, and insert operations, returns an empty dictionary upon a successful update. Returned: always Sample: |