Python OpenPyXL Write Excel xlsx File with Write Only Mode

In this Python openpyxl tutorial we learn how to write Excel xlsx files with write only mode using the openpyxl package. This write only mode can be used when you have a large dataset to write that need optimise the performance.

How to install the openpyxl package

To install the openpyxl package using the command below.

pip install openpyxl

How to write Excel xlsx file using Workbook write only mode

Below is step by step how to use the write only mode.

Import the Workbook from the openpyxl package.

from openpyxl import Workbook

Create a new Workbook object with write_only mode is True.

wb = Workbook(write_only=True)

Create a new Worksheet object.

ws = wb.create_sheet()

Append data to the worksheet.

for i in range(100):
    ws.append([i])

Then save the Workbook as test.xlsx file.

wb.save('test.xlsx')

The complete Python program as below.

write_only_xlsx.py

from openpyxl import Workbook

wb = Workbook(write_only=True)
ws = wb.create_sheet()

for i in range(100):
    ws.append([i])

wb.save('test.xlsx')

Execute the above Python program we will get the Excel file test.xlsx as below screenshot.

Python openpyxl Write Excel xlsx File with Write Only Mode

How to use WriteOnlyCell

You can also use WriteOnlyCell to create write only mode cells, for example

cell = WriteOnlyCell(ws, value='Tori Code')
ws.append([cell])

The complete Python program as below.

write_only_cell.py

from openpyxl import Workbook
from openpyxl.cell import WriteOnlyCell

wb = Workbook(write_only=True)
ws = wb.create_sheet()

cell = WriteOnlyCell(ws, value='Tori Code')
ws.append([cell])

wb.save('write_only_cell.xlsx')

Execute the above Python program we will get the Excel file write_only_cell.xlsx as below screenshot.

Python openpyxl Write Excel xlsx File with Write Only Mode

Happy Coding 😊