#summon a book, give it the item's enchantments
summon item ~ ~ ~ {Tags:["EmptyBookKyu"],Item:{id:"minecraft:enchanted_book",Count:1b}}
data modify entity @e[type=item,tag=EmptyBookKyu,sort=nearest,limit=1] Item.tag.StoredEnchantments append from entity @s Item.tag.Enchantments[0]
data modify entity @e[type=item,tag=EmptyBookKyu,sort=nearest,limit=1] Item.tag.StoredEnchantments append from entity @s Item.tag.Enchantments[1]
data modify entity @e[type=item,tag=EmptyBookKyu,sort=nearest,limit=1] Item.tag.StoredEnchantments append from entity @s Item.tag.Enchantments[2]
data modify entity @e[type=item,tag=EmptyBookKyu,sort=nearest,limit=1] Item.tag.StoredEnchantments append from entity @s Item.tag.Enchantments[3]
data modify entity @e[type=item,tag=EmptyBookKyu,sort=nearest,limit=1] Item.tag.StoredEnchantments append from entity @s Item.tag.Enchantments[4]
data modify entity @e[type=item,tag=EmptyBookKyu,sort=nearest,limit=1] Item.tag.StoredEnchantments append from entity @s Item.tag.Enchantments[5]
data modify entity @e[type=item,tag=EmptyBookKyu,sort=nearest,limit=1] Item.tag.StoredEnchantments append from entity @s Item.tag.Enchantments[6]
data modify entity @e[type=item,tag=EmptyBookKyu,sort=nearest,limit=1] Item.tag.StoredEnchantments append from entity @s Item.tag.Enchantments[7]

#if any enchantment is higher than level 5, set it to 5
execute as @e[type=item,tag=EmptyBookKyu,sort=nearest,limit=1] run scoreboard players set @s KyuDummy 0
execute store result score @e[type=item,tag=EmptyBookKyu,sort=nearest,limit=1] KyuDummy run execute as @s run data get entity @s Item.tag.StoredEnchantments[0].lvl
execute as @e[type=item,tag=EmptyBookKyu,sort=nearest,limit=1,scores={KyuDummy=6..}] run data modify entity @s Item.tag.StoredEnchantments[0].lvl set value 5
execute as @e[type=item,tag=EmptyBookKyu,sort=nearest,limit=1] run scoreboard players set @s KyuDummy 0
execute store result score @e[type=item,tag=EmptyBookKyu,sort=nearest,limit=1] KyuDummy run execute as @s run data get entity @s Item.tag.StoredEnchantments[1].lvl
execute as @e[type=item,tag=EmptyBookKyu,sort=nearest,limit=1,scores={KyuDummy=6..}] run data modify entity @s Item.tag.StoredEnchantments[1].lvl set value 5
execute as @e[type=item,tag=EmptyBookKyu,sort=nearest,limit=1] run scoreboard players set @s KyuDummy 0
execute store result score @e[type=item,tag=EmptyBookKyu,sort=nearest,limit=1] KyuDummy run execute as @s run data get entity @s Item.tag.StoredEnchantments[2].lvl
execute as @e[type=item,tag=EmptyBookKyu,sort=nearest,limit=1,scores={KyuDummy=6..}] run data modify entity @s Item.tag.StoredEnchantments[2].lvl set value 5
execute as @e[type=item,tag=EmptyBookKyu,sort=nearest,limit=1] run scoreboard players set @s KyuDummy 0
execute store result score @e[type=item,tag=EmptyBookKyu,sort=nearest,limit=1] KyuDummy run execute as @s run data get entity @s Item.tag.StoredEnchantments[3].lvl
execute as @e[type=item,tag=EmptyBookKyu,sort=nearest,limit=1,scores={KyuDummy=6..}] run data modify entity @s Item.tag.StoredEnchantments[3].lvl set value 5
execute as @e[type=item,tag=EmptyBookKyu,sort=nearest,limit=1] run scoreboard players set @s KyuDummy 0
execute store result score @e[type=item,tag=EmptyBookKyu,sort=nearest,limit=1] KyuDummy run execute as @s run data get entity @s Item.tag.StoredEnchantments[4].lvl
execute as @e[type=item,tag=EmptyBookKyu,sort=nearest,limit=1,scores={KyuDummy=6..}] run data modify entity @s Item.tag.StoredEnchantments[4].lvl set value 5
execute as @e[type=item,tag=EmptyBookKyu,sort=nearest,limit=1] run scoreboard players set @s KyuDummy 0
execute store result score @e[type=item,tag=EmptyBookKyu,sort=nearest,limit=1] KyuDummy run execute as @s run data get entity @s Item.tag.StoredEnchantments[5].lvl
execute as @e[type=item,tag=EmptyBookKyu,sort=nearest,limit=1,scores={KyuDummy=6..}] run data modify entity @s Item.tag.StoredEnchantments[5].lvl set value 5
execute as @e[type=item,tag=EmptyBookKyu,sort=nearest,limit=1] run scoreboard players set @s KyuDummy 0
execute store result score @e[type=item,tag=EmptyBookKyu,sort=nearest,limit=1] KyuDummy run execute as @s run data get entity @s Item.tag.StoredEnchantments[6].lvl
execute as @e[type=item,tag=EmptyBookKyu,sort=nearest,limit=1,scores={KyuDummy=6..}] run data modify entity @s Item.tag.StoredEnchantments[6].lvl set value 5
execute as @e[type=item,tag=EmptyBookKyu,sort=nearest,limit=1] run scoreboard players set @s KyuDummy 0
execute store result score @e[type=item,tag=EmptyBookKyu,sort=nearest,limit=1] KyuDummy run execute as @s run data get entity @s Item.tag.StoredEnchantments[7].lvl
execute as @e[type=item,tag=EmptyBookKyu,sort=nearest,limit=1,scores={KyuDummy=6..}] run data modify entity @s Item.tag.StoredEnchantments[7].lvl set value 5
execute as @e[type=item,tag=EmptyBookKyu,sort=nearest,limit=1] run scoreboard players set @s KyuDummy 0

tag @e[type=item,tag=EmptyBookKyu,sort=nearest,limit=1] remove EmptyBookKyu

kill @s[type=item]