5. Files and images (upload / download / delete / export)
Uploading, downloading, and deleting attachments, adding images to the body, and exporting as PDF / ELN / JSON. Experiments and Resource items work the same way.
The short version
files = elab.items.entry(item_id).files # for an Experiment: elab.experiments.entry(experiment_id).files
files.upload("coa.pdf") # upload (skipped if the same name and content already exist)
files.list() # list attachments
files.download_all("output/download") # download everythingUploading files
uploaded_files = elab.experiments.entry(experiment_id).files.upload(
["./tests/test_file/upload_test.txt"], # a single str or Path also works
comment="Uploaded with APIv2",
overwrite=True,
skip_if_same=True,
verify_after_upload=True,
debug=True,
)| Argument | Default | Meaning |
|---|---|---|
comment | "Uploaded with APIv2" | Comment on the attachment |
overwrite | True | Replace an existing file with the same name |
skip_if_same | True | Do not upload when a file with the same name and content exists (re-running gives the same result) |
verify_after_upload | True | Verify after upload using a hash, etc. |
The return value is the list of file names (real_name) actually uploaded. Files skipped because of skip_if_same=True are not included.
Downloading attachments
files = elab.experiments.entry(experiment_id).files
# everything into a folder
saved_files = files.download_all("output/download", overwrite=False, verify_after_download=True)
# everything into one zip
zip_path = files.download_all_zip(
"output/experiment_files.zip",
overwrite=True,
verify_after_download=True,
include_manifest=True,
)
# by file name
saved_file = files.download_by_name("file_upload_test.txt", output_dir="output/download", overwrite=False)
# by upload ID
saved_file = files.download(upload_id=123, output_path="output/download/file_upload_test.txt")download_all_zip() stores the attachments under attachments/ in the zip with their original names. With include_manifest=True, manifest.csv and manifest.json are added too. .eln export is for exporting / importing the whole eLabFTW record; download_all_zip() is for getting just the attachments with human-friendly names.
Attachments of Resource items are handled the same way through elab.items.entry(item_id).files.
Deleting attachments
elab.experiments.entry(experiment_id).files.delete(upload_id=123)
elab.experiments.entry(experiment_id).files.delete_by_name("file_upload_test.txt")
elab.items.entry(item_id).files.delete(upload_id=123)
elab.items.entry(item_id).files.delete_by_name("file_upload_test.txt")
# delete everything (confirm=True is required to prevent accidents)
elab.experiments.entry(experiment_id).files.delete_all(confirm=True)
elab.items.entry(item_id).files.delete_all(confirm=True)Adding images to the body: images.append()
Uploads image files and appends <img> tags to the end of the body.
image_files = [
"./tests/test_file/images/test_image1.png",
"./tests/test_file/images/test_image2.png",
]
uploaded_file_names = elab.experiments.entry(experiment_id).images.append(
image_files,
image_captions=["image1", "image2"],
image_width=None,
image_height=None,
image_scale=0.5,
image_align="center",
image_style="border:1pt solid black",
image_caption_style="font-size:90%; color:#555;",
remove_existing=True,
overwrite=True,
skip_if_same=True,
verify_after_upload=True,
debug=True,
)
# same for Resource items
elab.items.entry(item_id).images.append(image_files, image_captions=["image1", "image2"], image_scale=0.5)remove_existing=True: remove existing<img>tags for images with the same name from the body before appending.overwrite=True: replace an already uploaded image with the same name.skip_if_same=True: do not re-upload images with the same name and content.image_scale: scale the displayed size (Pillow is used to compute the size).image_captions: add a caption below each image.
Exporting as PDF / ELN / JSON: download()
saved_files = elab.experiments.entry(experiment_id).download(
experiment_title="test_experiment",
file_path="output",
formats=("pdf", "eln", "json"),
)
saved_files = elab.items.entry(item_id).download(
item_title="test_item",
file_path="output",
formats=("pdf", "eln", "json"),
)
print(saved_files)Output file names have the form <id>_<sanitized_title>_<timestamp>_export.<format>:
99_test_experiment_2026-06-02_10-20-28_export.pdf
99_test_experiment_2026-06-02_10-20-28_export.eln
99_test_experiment_2026-06-02_10-20-28_export.jsonSaved files are reported via logging (INFO).
Looking inside an ELN file
An .eln file is actually a ZIP archive. Keep it as .eln to re-import into eLabFTW, but to look at the contents you can rename it to .zip and extract it.
cp 99_test_experiment_2026-06-02_10-20-28_export.eln \
99_test_experiment_2026-06-02_10-20-28_export.zip
unzip 99_test_experiment_2026-06-02_10-20-28_export.zip -d eln_exportAfter extraction you will find an HTML file for the body and JSON files with metadata, attachment information, and so on. Keep the .eln file for sharing, archiving, and re-import, and extract it as ZIP only for inspection or debugging.
Pitfalls
delete_all(confirm=True)deletes all attachments at once. Test it on test entries first.overwrite,skip_if_same, andverify_after_uploadcontrol how files with the same name are handled and whether uploads are verified.- Depending on eLabFTW settings, Japanese (CJK) text may be garbled in PDF export. Enable a CJK font in eLabFTW under "Settings > General > PDF settings".
- Changes to a locked entry may be rejected. In that case, do the work inside
with entry.unlocked():(8. Lock).
Next: 6. Links and tags.