Nov 17 (Reuters) - Spain's trade deficit narrowed 45% in the first nine months of 2023 from the same period a year earlier, to 29.6 billion euros ($32.1 billion) as a result of lower and cheaper energy imports, industry ministry data showed on Friday.

Imports over the January-September period fell 6.7% to 317.18 billion euros, while exports rose 0.3% to 287.59 billion euros, the ministry said.

The value of energy imports, mainly oil and natural gas, fell 32% in the first nine months of the year compared with the same period a year ago as Spain bought 18% less of it at prices 17.2% cheaper.

Spain's deficit has been shrinking throughout 2023 after it widened rapidly in 2022 as oil and natural gas prices soared following the Russian invasion of Ukraine.

Spain relies entirely on imports for its oil and gas, representing a chronic deficit for the country, according to Madrid-based think-tank Elcano Royal Institute.

Other imports during the period fell 0.4% from the same period a year ago. ($1 = 0.9220 euros) (Reporting by Jakub Olesiuk, editing by Inti Landauro and Hugh Lawson)