use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager();
$categoryFactory = $obj->get('\Magento\Catalog\Model\CategoryFactory');
$categoryName = 'My Category';
$parentId = 2; // Parent category ID
$category = $categoryFactory->create();
$category->setName($categoryName);
$category->setIsActive(true);
$category->setParentId($parentId);
$category->setPath($parentId . '/' . $category->getUrlKey());
$category->save();
请确保将父类别 ID 和类别名称替换为自己的值。如果是多级目录,需要用到递归的方式。