Manual
EN / JA

Build a script with AI

Let’s build a real Pybes script together, using an AI chat to write the code.

The task — merge every Excel file in a folder into one

You have a folder full of .xlsx files and you want to consolidate them into a single Excel workbook. It’s the kind of thing that comes up constantly in office work.

In this walkthrough, we’ll have the AI write a script that merges the files, keeping each source file’s contents on its own sheet.

Step 1 — Generate a prompt in Pybes

  1. Create a new script
  2. In the editor toolbar, click the Prompt button
  3. The prompt is copied to your clipboard
Tip: You don’t need to set up fields yet. Pybes will auto-detect them from the code later.

Step 2 — Tell the AI what you want

  1. Open an AI chat (ChatGPT, Claude, Gemini, etc.)
  2. Paste the prompt
  3. At the end of the prompt, write something like:

Read every Excel file (.xlsx) in the specified folder and
merge them into a single Excel file, saved to a specified path.
Keep each file’s contents on its own sheet, named after the source file.

Write this part in whatever language you think in, but be specific. “Merge Excel files” isn’t enough — say exactly how you want them merged (separate sheets, appended rows, etc.).

Step 3 — Paste the generated code into Pybes

The AI will produce something like this:

import sys
import json
import os
from openpyxl import load_workbook, Workbook

with open(sys.argv[1], encoding="utf-8") as f:
    inputs = json.load(f)

input_folder = inputs["input_folder"]
output_file = inputs["output_file"]

wb_out = Workbook()
wb_out.remove(wb_out.active)

files = [f for f in os.listdir(input_folder) if f.endswith(".xlsx")]
print(f"Found {len(files)} Excel file(s)")

for i, fname in enumerate(files, 1):
    path = os.path.join(input_folder, fname)
    wb_in = load_workbook(path)

    for sheet in wb_in.sheetnames:
        ws_in = wb_in[sheet]
        sheet_name = os.path.splitext(fname)[0]
        if sheet_name in wb_out.sheetnames:
            sheet_name = f"{sheet_name}_{sheet}"
        ws_out = wb_out.create_sheet(title=sheet_name[:31])

        for row in ws_in.iter_rows(values_only=False):
            for cell in row:
                ws_out.cell(row=cell.row, column=cell.column, value=cell.value)

    print(f"[{i}/{len(files)}] Loaded {fname}")

wb_out.save(output_file)
print(f"Merge complete: {output_file}")

Paste this straight into the Pybes editor.

Step 4 — Variables are auto-detected

The moment you paste, Pybes detects inputs["input_folder"] and inputs["output_file"]:

  • The detected variables appear on the right side of the editor
  • Click them to add as config fields
  • Change input_folder to the Folder type
  • Change output_file to the File type and set the extension to xlsx

Step 5 — Fill in the values and run

  1. Point input_folder at the folder with your Excel files
  2. Set output_file to where you want the merged file saved
  3. Click Run
  4. The console shows progress as files are merged

If something goes wrong

  • If the console shows an error, click the Consult button
  • A debug-ready prompt is copied. Paste it into the AI chat
  • The AI reviews the error and fixes the code
Tip: The Consult prompt includes the output, the code, and the inputs — all the context the AI needs. You don’t have to explain the error yourself.

Take it further