Pelosi, the first woman to hold the speaker’s gavel, has represented San Francisco for nearly 40 years.