Former News Of The World reporter and royal editor Clive Goodman told the Old Bailey how the princess, who separated from Prince Charles in 1992 after 11 years, passed him information relating to the royal household and staff in an envelope.
Mr Goodman said: "She was going through a very, very difficult time.
"She told me she wanted me to see the scale of her husband's staff and household, compared with others.
"She felt she was being swamped by people close to his household. She was looking for an ally to take him on - to show there were forces that would rage against him."
Mr Goodman, 56, of Surrey, denies two counts of conspiracy to commit misconduct in a public office.
The court has already heard how former News Of The World and Sun editor Rebekah Brooks offered Mr Goodman a backroom job in 2007 following his release from prison for his part in phone hacking, but he declined the offer.
Mr Goodman told the court he used "Green Books" and internal telephone directories containing contact numbers for royal staff and senior members of the household, for stories.
He recalled how one Green Book was given to him in 1992 by the princess.
Mr Goodman said: "That arrived at my office in Wapping with my name on it.
"She had a relationship with several journalists - Richard Kay at the Daily Mail, Martin Bashir of Panorama."
He said the royal had been going through "a very tough time" - only to be cut off by judge Mr Justice Saunders and asked to stick to what she had told him.
Mr Goodman said he did not pay for the books.
Mr Goodman, who was dismissed in 2007 following his conviction, and six other defendants, including Mrs Brooks, deny all charges.
The trial continues.