Exporting a Huge Sheet? The Trade-Offs of write_only Mode
writes to the worksheet only via sequential appending and does not support random access by coordinates. You should first define the relevant layout, then appen
Exporting a very large worksheet from Python can strain memory, and openpyxl offers a write-only mode for this case. This guide is for developers who need to write big spreadsheets and are deciding whether that mode is worth its restrictions. It explains what write-only mode trades away, the order of operations it requires, and how to check the output before you rely on it.
You will learn how the sequential writing model works, which settings must be made before any data is written, and which features the mode cannot handle. You will also get an acceptance checklist for judging whether an exported file is usable.
They do not guarantee results for every real-world file. Keep the original copies of important files, and verify your output against the boundaries and acceptance criteria below.
Should you use write-only mode for a huge sheet? The short answer
Use write-only mode when you export a large sheet in one pass, you can define the layout before writing, and you do not need to go back and edit cells.
| Your situation | What to do | Why |
|---|---|---|
| Huge sheet, and the data is final before writing | Use write_only mode and append rows sequentially | It serializes XML fragments to disk, keeping memory use constant, typically below 10MB |
| You need to edit a cell after writing it | Stay in normal mode, or prepare the final rows before writing | write_only mode has no coordinate access, so earlier cells cannot be reached |
| You need frozen panes, column widths or auto-filters | Set them before writing any cell | The XLSX format requires these definitions before the data nodes |
| You need styles on cells | Attach styles through WriteOnlyCell before calling append() | Once a row is appended, its style information cannot be changed |
| Your template has merged cells, images, charts or data validation | Revert to normal mode | write_only mode does not support these elements |
How write-only mode works
A one-way stream
write_onlymode serializes XML fragments from an XLSX file directly to disk, instead of building a full DOM tree in memory.- The worksheet loses the ability to reference earlier data. You add rows with
append(). - Methods such as
cell(),iter_rows()and any other coordinate-based access are not available. - The design gives you constant memory usage, typically below 10MB, but you give up backtracking and random editing.
Think of it as a printer's paper feed slot, not an erasable whiteboard.
Follow the required order
- Define the worksheet-level metadata: frozen panes, column widths and auto-filters.
- Prepare any styles as
WriteOnlyCellobjects. - Append rows with
append(). - Save once.
- To redo the export, recreate the workbook. You cannot reuse it after saving.
Do not rely on any specific exception type raised when repeated saves fail. Your acceptance checks should cover this case directly, as described in the checklist below.
Why the order matters
The XLSX format requires the metadata to appear before the data nodes in the sheet XML. If the order is reversed, the file may become corrupted or lose its formatting.
Prepare your data before writing
If your logic needs to change a specific cell based on later data, for example changing a cell in row 1000 while you are writing row 5, write_only mode does not fit. Instead:
- Complete all data cleaning, aggregation and formatting calculations in memory or in a database.
- Produce a final-state row iterator.
- Feed that iterator to
append().
If your source data needs de-identification or field alignment, finish it beforehand. The data structure cannot be revised once writing has begun.
Set styles and metadata in advance
In normal mode, you can change the font or borders of any cell at any time. In write-only mode:
- Styles must be attached to
WriteOnlyCellobjects. - Styles must be configured before the row is passed to
append(). - Worksheet metadata such as
freeze_panes, column widths and auto-filters must be set before any cell is written.
Install lxml for faster writes
The official documentation recommends installing the lxml library to improve write performance. If it is missing, openpyxl falls back to a pure Python implementation. Functionality is unchanged, but processing data on the order of millions of rows may take several times longer.
A self-contained synthetic example
The example lists its dependencies in the import list. It builds its inputs in memory.
from io import BytesIO
from openpyxl import Workbook, load_workbook
from openpyxl.cell import WriteOnlyCell
from openpyxl.styles import Font
wb = Workbook(write_only=True)
ws = wb.create_sheet("Export")
ws.freeze_panes = "A2"
header = WriteOnlyCell(ws, value="Order")
header.font = Font(bold=True)
ws.append([header, "Amount"])
ws.append(["ORD-1", 99.5])
try:
ws["A1"]
except TypeError:
pass
else:
raise AssertionError("write-only worksheet allows random access")
out = BytesIO()
wb.save(out)
restored = load_workbook(BytesIO(out.getvalue()))
assert restored.active["A1"].value == "Order"
assert restored.active["A1"].font.bold
assert restored.active["B2"].value == 99.5
assert restored.active.freeze_panes == "A2"
Correct streaming writes and boundary checks
The synthesis example shows how to build data in memory, apply styles safely, and include assertion checks for common errors. It is a check of logical correctness, not a performance benchmark. For real use, replace the sample data with the path to your real file.
Acceptance checklist and manual review boundary
Code that runs without errors does not mean the exported file is usable. Use the checklist below to separate automated checks from cases that need manual review.
| Check item | Automation/code demonstration | End-to-end acceptance | Does not apply / needs manual review |
|---|---|---|---|
| File accessibility | save() raises no exceptions | Excel or WPS opens the file without a repair prompt | Truncation behaviour beyond 1,048,576 rows |
| Style rendering | WriteOnlyCell styles have been assigned | Header is bold, and the frozen first row takes effect | Complex conditional formatting, charts, macros |
| Data integrity | Row and column count assertions match | Sample-check numerical precision and encoding | Special characters, line breaks, right-to-left text |
| Memory usage | Process monitoring stays under your threshold | No out-of-memory errors with large data volumes | Peak memory when used with other libraries |
| Repeated operations | Code catches the AlreadySaved exception | Confirm the old file is not silently overwritten | Incremental update and partial refresh requirements |
| Advanced features | The relevant API is not called in the code | Confirm no merged cells or images are missing | Template inheritance, data validation, VBA |
Special note: write-only mode does not support merged cells, images, charts or data validation. If your report template includes these elements, forcing this mode will lose content. In that case, return to normal mode, or separate the data from the template.
Test your own output
In your test environment, run a variant of the synthesis example on a real dataset of 500,000 rows with header formatting. Record the peak memory usage and the time to open the file.
If you see style loss or file corruption, work through the acceptance checklist item by item. Check whether any of these has been affected: the metadata order, the styles, or a feature that falls outside what write-only mode supports.
Reference: openpyxl Optimised Modes Official Documentation
Use this checklist for document translation
The same checklist thinking applies to document translation:
- Identify which elements are text, which are formulas, and which are business codes.
- Translate the tables.
- Verify the result. Check that the text is correctly rendered in the target language, and that formulas, identifiers and citation relationships remain unchanged.
OmniDoc translates PDF, Word, PowerPoint and Excel files: scanned pages are recognized first, table cells are translated one by one so tables keep their rows and columns, and a PDF comes back as both a bilingual and a translation-only file. Paid translation adds a glossary and context translation.
📊 At a glance
| Feature | Normal mode | Write-only mode |
|---|---|---|
| Edit any cell at any time | Yes | No, rows are appended sequentially |
| Change a cell's style after it is appended | Yes | No |
| Set frozen panes and column widths | At any time | Before writing any cell |
| Merged cells, images, charts, data validation | Supported | Not supported |
| Memory use | Not covered in this guide | Constant, typically below 10MB |
🎯 Which approach fits you?
If you are exporting a huge, final dataset in one pass
Use write-only mode. Define the metadata, then append the rows.
If you need to revise cells based on later data
Stay in normal mode, or finish the calculations before you write.
If your template includes merged cells, images or charts
Use normal mode, or separate the data from the template.
If you are unsure whether your output is usable
Run the synthesis example on a real real sample, then check the acceptance checklist.
❓ FAQ
1. Can I edit a cell after I have appended it in write-only mode?
No. Write-only mode writes sequentially and does not support coordinate access, so earlier cells cannot be changed. Prepare the final rows before you append them.
2. Why must I set frozen panes and column widths before writing?
The XLSX format requires these definitions to appear before the data nodes in the sheet XML. If the order is reversed, the file may become corrupted or lose its formatting.
3. What should I do if I need to export the file again?
Recreate the workbook. After a save, the workbook cannot be reused for another export.
4. Does write-only mode support images or charts?
No. Merged cells, images, charts and data validation are not supported, so use normal mode for templates that include them.
5. Will my export be slower without lxml?
Functionality is unchanged, but processing data on the order of millions of rows may take several times longer with the pure Python fallback.
📝 Summary
Write-only mode suits large, final datasets that you can write in one pass. Define the metadata and styles before appending rows, and check the output against the acceptance checklist before relying on it.
Try it now: upload a document and get it translated with the layout intact


