A man wielding a large knife and shouting in Arabic targeted officers on Monday at a police station south of Barcelona, in what police are calling a terrorist attack.
The attacker, said to be 29 and of Algerian origin, was shot dead as he lunged towards officers, said police.
Spain's north-east Catalan region has been marking the anniversary of the 2017 jihadist attack on Barcelona.
However, police said they had found no evidence the incidents were linked.
The attacker, identified locally as Abdelouahab Taib, approached the police station in the town of Cornellà de Llobregat at 05:52 (03:52 GMT) on Monday, buzzing the intercom and demanding to be allowed in.
When a woman officer behind a reception window opened the door, the man lunged at her "with a clear intent to kill", Police Commissioner Rafel Comes told reporters.
The man shouted "Allahu Akbar" (God is greatest) as he rushed forward.
The officer opened fire and the attacker was fatally wounded, the commissioner said, without saying how many shots were fired. The officer had used a gun to save her life, he said, although it was unclear if she was behind a glass partition at the time.
After the police station attack in Cornellà, we asked to all the police officers use best possible measures security and self-protection— Mossos (@mossos) August 20, 2018
"For now we are treating it as a terrorist attack," he said, although he stressed there was nothing at this stage to link him to the jihadist cell involved in the attacks on Barcelona's Las Ramblas venue on 17 August last year and Cambrils hours later.
Sixteen people were killed in the twin attacks and the man who drove a van into civilians on Las Ramblas was eventually tracked down and shot dead on 21 August.
Abdelouahab Taib had been living in Spain for several years and had a foreigners' identity number. He had no criminal record and lived with a Spanish woman, although local media reported that they had begun divorce proceedings.
Members of the Catalan police force and other authorities searched the man's home a short distance from the police station while his partner was questioned.
Police stations across Spain were put on alert as a result of Monday's incident.
Spain has been on its second highest terrorist alert level - four out of five - since June 2015.
Teresa Cunillera, the Spanish government's representative in Catalonia, had earlier refused to be drawn on the motive for Monday's incident until police had investigated further.
"Reaching conclusions is very hard before they have carried out the minimum of checks and looked into the motives," she told Spanish radio.