Don't re-save the client's Excel directly: check external links first

keep_vba=True is a retention policy, not evidence that a macro was "detected." The check below only inventories whether the ZIP package contains a vbaProject.bin and external link parts. The presence of a part does not mean the content is safe, that the macro is valid, or that the external target is reachable. No macros will be executed and no links will be accessed.

Key Points of This Article

  • keep_vba=True is a retention policy and cannot serve as evidence that a macro was detected in the file.
  • By default, openpyxl sets keep_vba to False; loading and then saving directly removes the VBA project.
  • keep_links defaults to True, which only preserves link definitions and does not guarantee that external data caches remain valid after re-saving.
  • In read-only mode, the keep_links and keep_vba parameters have no effect, so operations attempting to preserve these features will fail.
  • The inspection only inventories the VBA and external link parts within the ZIP package; the presence of a part does not mean its content is safe or that the macro is valid.

The Excel file a customer sent opens normally on their local machine. However, after it has been processed by a Python script or uploaded to a system, formulas may turn into #REF!, drop-down lists may disappear, or the file may fail to open altogether. This is usually not caused by file corruption. Instead, the automation tool did not fully preserve the external link cache or VBA macros when re-saving the file.

This article is written for developers and operations staff who use scripts to batch-process Excel files. It explains what information openpyxl drops by default when loading a file, gives an inspection approach using a synthetic sample, and lists the boundary cases that need manual review during acceptance. After reading it, you should be able to determine what to check before re-saving a file.

One-line conclusion: Before re-saving an Excel file, inventory external links and VBA components first

First confirm whether the file package contains VBA projects and external link parts, then decide the loading parameters, and keep a manual review step during acceptance.

Your situationRecommended PracticesCause
vbaProject.bin exists in the document packageNote that it contains content requiring separate review; macros are not executed.The existence of a component does not mean the macro is valid or its content is safe
The document package contains external link partsRecord its position within the package, without accessing the target.The script only takes inventory; it does not verify whether the targets are accessible.
Load with keep_vba=TrueUnderstand it as a loading strategy.keep_vba=True is a retention policy, not evidence that a macro was detected

Why Default Loading Loses Critical Information

According to the official openpyxl documentation, load_workbook has clear boundaries in how it handles non-cell data. keep_links defaults to True, which preserves the link definitions in the XML but does not guarantee that external data caches (Cached Values) remain valid after re-saving. Once the file is separated from the original network environment or source file path, Excel may still report errors when refreshing.

keep_vba defaults to False. If the original file contains macros, the VBA project will be removed when it is loaded with the defaults and then saved. Charts, shapes, and other objects may also be ignored when reading.

These aren't bugs; they are the library's actual functional boundaries. When handling production-grade files, you should explicitly declare the retention policy rather than assume the tool will automatically preserve all features.

Synthetic Examples and Test File Preparation

To demonstrate this mechanism safely, it is recommended to construct a synthetic scenario that does not rely on real customer data.

Prepare test files

  1. Create a new Excel workbook.
  2. In the VBA editor (Alt+F11), insert a blank module.
  3. Enter the formula ='[ExternalBook.xlsx]Sheet1'!$A$1 in cell A1. There is no need to create an actual external file; just keep this reference structure.
  4. Save as a macro-enabled format input_check.xlsm.

Common Errors and Edge-Case Inputs

  • Loading a macro-enabled file with the default load_workbook('input_check.xlsm') and then saving it as .xlsx silently discards the VBA code.
  • If keep_vba=True is not specified at load time but the file is forcibly saved as .xlsm, Excel may refuse to open it because the file header does not match its actual content.
  • In read-only mode (read_only=True), the keep_links and keep_vba parameters have no effect, so any attempt to preserve features will fail.

Validation code for preserving external links and macros

The example below builds the check logic in memory to demonstrate the role of the parameters.

from io import BytesIO
from zipfile import ZipFile
from openpyxl import Workbook
def inventory(raw):
    with ZipFile(BytesIO(raw)) as z:
        names = set(z.namelist())
    return {"has_vba_part": "xl/vbaProject.bin" in names,
            "external_link_parts": sorted(n for n in names if n.startswith("xl/externalLinks/externalLink") and n.endswith(".xml"))}
out = BytesIO()
Workbook().save(out)
assert inventory(out.getvalue()) == {"has_vba_part": False, "external_link_parts": []}
synthetic = BytesIO()
with ZipFile(synthetic, "w") as z:
    z.writestr("xl/vbaProject.bin", b"not executable VBA; fixture only")
    z.writestr("xl/externalLinks/externalLink1.xml", b"<fixture/>")
result = inventory(synthetic.getvalue())
assert result["has_vba_part"]
assert result["external_link_parts"] == ["xl/externalLinks/externalLink1.xml"]

This code is only a teaching example, not a complete file repair tool, and it has not been run in a real production environment. Before running it, make sure you have prepared input_check.xlsm as described above.

📊 Core Comparison: Default Parameters vs. Retained Parameters

ParametersDefault valueFunctionBoundary
keep_linksTruePreserve link definitions in XMLExternal data cache is not guaranteed to remain valid after a restart.
keep_vbaFalseKeep VBA project off by defaultSetting it to True does not mean the macro content is safe or valid.
read_onlyFalseLoad in read-only modeUnder this mode, keep_links and keep_vba have no effect.

Acceptance Criteria and Boundaries for Manual Review

Automated processing cannot cover all of Excel's edge cases. The table below distinguishes which cases are handled by the script for inventory, and which cases need to be intercepted or handled manually.

ObserveWhat should be doneShould not be inferred
VBA componentContent requiring separate review is included in the promptDo not run macros
External link componentRecord position within packageDo not visit the target
Reserved parameter enabledTreat as loading strategyNot treated as evidence of detected macros

Scripts can verify whether parameters take effect, but they cannot guarantee that the output file is usable at the business level. For example, keep_links=True successfully preserved the link definition, yet if the target server has been taken offline, the user will still get an error when refreshing in Excel. Problems of this kind go beyond the responsibility of a document-processing library, so the acceptance process needs to reserve a manual review step.

References

Using this checklist for document translation

When handling tables that contain formulas and business codes, first distinguish text from formulas, identifiers, and business codes, then translate. After translating, check not only that the Chinese has been converted into the target language, but also that formulas, identifiers, and reference relationships have not been changed.

OmniDoc's document translation supports formats such as PDF, Word, PPT, and Excel. Scanned documents have their text recognized first, tables are translated cell by cell with rows and columns preserved, and PDFs produce two versions: a bilingual side-by-side version and a translation-only version. Paid translation also lets you upload a glossary and enable context-aware translation.

🎯 Which Approach Suits You?

If you need to batch-process files that contain macros

First define the retention policy explicitly with keep_vba=True, then compare the output against the expected results in a test environment. Do not assume that macros will be preserved automatically.

If Your Excel File Contains External Links

First record the location of the link parts within the package, and do not access the link targets in your script. After re-saving, manually confirm whether refresh works correctly.

If you only need to read data

You may consider read-only mode, but note that the keep_links and keep_vba parameters have no effect in this mode.

If the file comes from a client and contains sensitive content

First keep the original file intact, run tests using real samples, and then verify the results item by item against the acceptance criteria.

❓ Frequently Asked Questions (FAQ)

1. Does keep_links=True mean external links are guaranteed to work?

No. keep_links preserves the link definitions in the XML, but it does not guarantee that the external data cache remains valid after re-saving; if the target is inaccessible, refreshing may still produce errors.

2. What happens to macros when a file is loaded by default and then saved?

keep_vba defaults to False, and the VBA project in the original file will be removed when saved. If you need to keep it, you should explicitly set keep_vba=True.

3. When a VBA component is detected, can the script determine whether the macro is safe?

No. The presence of a component does not mean its content is safe or that the macro is valid. The script should not execute the macro, and should instead indicate that a separate review is required.

4. Can links and macros be preserved in read-only mode?

No. In read_only=True mode, the keep_links and keep_vba parameters have no effect.

📝 Summary

Before re-saving the Excel files, inventory the VBA and external link components, clarify the loading parameters, and keep a manual review step. Next, in a test environment, prepare three sample files (a pure-data .xlsx, a macro-enabled .xlsm, and an .xlsx containing external links). Load and save each one with the default parameters and with the retained parameters, then record the actual results against the table above. Only once you clearly understand the effect of each parameter on your file set is it appropriate to deploy the script into the production workflow.

Try it now: upload a document and start translating right away, with the original layout preserved.

Updated 2026-11-29