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
- Create a new script
- In the editor toolbar, click the Prompt button
- The prompt is copied to your clipboard
Step 2 — Tell the AI what you want
- Open an AI chat (ChatGPT, Claude, Gemini, etc.)
- Paste the prompt
- 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_folderto the Folder type - Change
output_fileto the File type and set the extension toxlsx
Step 5 — Fill in the values and run
- Point
input_folderat the folder with your Excel files - Set
output_fileto where you want the merged file saved - Click Run
- 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
Take it further
- Browse the scripts page for ready-to-use examples
- Other ideas: CSV conversions, batch file renaming, PDF merging
- Your finished scripts can be exported as .pybes files and shared with your team
- Read more about AI prompting in Prompt generator