The shortfall of exports to imports for the eurozone's fourth-biggest economy fell to €786.7 million ($1.07 billion), the economy ministry said.
Spain exported goods and services worth 19.86 billion euros, a record for July and a 1.3-percent rise on the same period a year ago.
Imports reached €20.65 billion , three percent lower than in July 2012.
Prime Minister Mariano Rajoy's conservative government is banking on exports to be an engine of recovery for Spain, which has been in recession for two years.
Spain's trade balance in March logged its first surplus since 1971, mainly due to a sharp fall in imports driven by falling domestic demand.
In July, exports such as chemicals products, iron and steel rose while imports of raw materials fell sharply, the ministry's figures showed.
Over the first seven months of 2013, Spain's trade deficit plunged 67.5 percent compared to the same period a year earlier, to 6.61 billion euros.
The volume of exports rose by seven percent to €138.58 billion, "a new historic high", the ministry said.