Create a top and bottom piece (the sum of the heights of the two parts should be equal or slightly larger than the battery height, you can divide it up any way you want).
Press fit or glue the magnets into the columns between the batteries. Make sure they're oriented correctly or you can use a magnet on one side and a steel bearing or pin on the other. The minimum size that works with magnets is 2x2 batteries. You can generate carriers for other sizes (like 1x4), but there won't be a slot for the magnet.
for reference, here are some common battery heights:
AA - 50.5mm
AAA - 44.5mm
CR123A - 34.5mm
CR2 - 27mm