Image copyright Getty Images

When Nancy Pelosi is sworn in as Speaker of the House on Thursday, she becomes not just the third most powerful US politician but also the leader of the Trump opposition. Both loved and loathed, her comeback story is an extraordinary tale of political survival.

After eight years in the political wilderness, Nancy Pelosi is back on top.

In 2007, the California Democrat made history as the first female speaker of the US House of Representatives, but it was short-lived.

This time, she's at the helm of a resurgent party with responsibility for initiating new laws through the lower chamber of Congress, not to mention guiding a slew of new investigations into the president.

And she's done so despite being written off multiple times and labelled a pedestrian public speaker prone to the occasional gaffe, having high disapproval ratings and becoming a lightning rod for Republicans.

Tying her name to embattled Democratic candidates had been an effective weapon for conservatives in the past but in the 2018 mid-term elections, it lost its punch.

Media playback is unsupported on your device Media caption The return of the woman Republicans love to hate

In Virginia, for example, Republican incumbent David Brat mentioned Nancy Pelosi and her "liberal agenda" 21 times in an hour and a half at a debate.

His Democratic opponent, Abigail Spanberger, finally shot back: "I question again whether Congressman Brat knows which Democrat in fact he's running against... My name is Abigail Spanberger."

She went on to win the district, one of 40 Democrats who captured Republican-held seats, giving the Democrats their largest surge in the House since the 1970s Watergate scandal.

Now, with her return to the speaker's chair, Ms Pelosi again becomes the most powerful woman in US politics.

It caps a remarkable journey for someone who grew up the youngest child in a family steeped in East Coast big-city politics, made a political name for herself in the most liberal corners of California and has dominated Democratic politics for nearly a decade and a half.

Media playback is unsupported on your device Media caption Testy exchanges between Trump, Pelosi and Schumer in the Oval Office

"People have gone wrong by under-estimating her for years," says journalist Elaine Povich, who wrote a 2008 biography about Ms Pelosi. "Never bet against her. She's consistently the hardest worker, the best organized and great vote counter."

These skills are going to be sorely tested in the days ahead, as the incoming speaker will have to balance the competing priorities of her Democratic caucus while facing incoming flames from the political Vesuvius that is Donald Trump.

The public had a taste of such confrontations in December, when the two argued in the Oval Office about border wall funding. She emerged from that duel with Democrats singing her praises but for many on the left such fireworks should only be the beginning.

They will be clamouring for aggressive oversight of the president while others want a legislative record that Democrats can run on.

It's a recipe for intra-party conflict and indicates the treacherous path ahead for her to navigate.

A political family

Although Republicans have typically painted Ms Pelosi as a "San Francisco liberal" enamoured with big government and far to the left on social issues, her roots are from a more practical style of politics on the other side of the continent.

She grew up in a political family, one of seven children in the gritty East Coast city of Baltimore, Maryland, where her father - Thomas "Big Tommy" D'Alesandro Jr - was mayor. She was the youngest and the only girl.

To be a politician in mid-century Baltimore meant succeeding at old-school Democratic machine politics. Keeping track of favours received and favours given. Knowing whom to help and whom to hurt - and how to do both. Ms Pelosi managed her family's political accounts, including answering the eight phone lines that connected to the house.

She went to college in nearby Washington where she met and eventually married financier Paul Pelosi. They first moved to Manhattan, and then San Francisco, where Ms Pelosi started as a housewife. She had five children - four daughters and a son - in the space of six years.

In 1976 she became involved in politics, using her old family connections to help then-California Governor Jerry Brown, running for president, win the Maryland primary.

She then rose through the state's Democratic Party ranks, eventually becoming its chair. In 1988 - at the urging of the outgoing Democrat - she ran for a seat in Congress and won.

In the House she worked her way up again. Because she represented a portion of the city with a large gay community, she made increasing Aids research funding a priority. She fought a multi-year bureaucratic battle to have a shuttered military base in San Francisco turned into a national park.

Image copyright Getty Images Image caption Congresswoman Pelosi watches on as President Bill Clinton signs an executive order in 1993

In 2001, she ran against Maryland's Steny Hoyer - whom she once interned with back in Washington - for House minority whip, vote-counter and second in command of the caucus, and won a narrow victory. The next year she moved up to minority leader after Dick Gephardt of Missouri resigned.

She was one of the highest-profile, most outspoken opponents of the US invasion of Iraq in 2003, and in 2005 successfully helped block President George W Bush's call for partial privatisation of the government-run Social Security retirement programme.

When the Democrats won the majority in 2006 for the first time in 12 years, her legislative acumen had been established and her stand on the war - at least in the minds of Democrats - was vindicated. She became the clear choice for Speaker of the House and was elected by her party in a unanimous vote.

In January 2007 the Californian made history as the first female speaker of the US House of Representatives.

But four years later, Democrats lost control of the lower chamber of Congress.

Despite the setback Ms Pelosi kept her head above the turbulent political waters, riding out a series of electoral defeats and beating back challenges within her own ranks, to take the gavel once more.

The power of the gavel

Speaker of the House is the one congressional job detailed in the US Constitution. It is second in line for the presidency, behind only the vice-president, although such an ascent would require an unlikely set of circumstances in which both offices were vacated.

Its massive office, in the Capitol rotunda, reflects the prestige of the job, with its own balcony looking out toward the Washington Monument.

Unlike the Senate, the majority party in the House - led by the speaker - has virtually unfettered control over the legislative process.

The speaker and her deputies and committee chairs determine what bills are considered and voted on. They set the agenda and decide the rules governing debate. If a speaker can keep her majority in line - and Republicans over the last two years showed that is far from a certainty - the legislative process in the House can purr like a well-tuned machine.

That was the case the last time Ms Pelosi was speaker.

From 2009 to 2011, when Democrats had unified control of Congress and the White House, her chamber enacted an $840bn stimulus package in the aftermath of the 2008 economic collapse. She passed pro-union and cap-and-trade climate legislation (which never made it past the Senate and into law) and financial reform and a bill prohibiting gender discrimination in pay (which did).

She also pushed hard to get the Affordable Care Act, which became the defining battle of the Barack Obama presidency, through the House and on to the president's desk.

Image copyright Alamy Image caption Deep in conversation with Barack Obama in 2014

Donna Edwards, then a member of the House from Maryland, describes Ms Pelosi's performance during the 2009 healthcare battles.

"In those negotiations, I watched Pelosi, the tactician," she wrote in a Washington Post opinion article.

"She held scores of meetings, back-to-back, day into night. She juggled phone calls - House and Senate leaders, Cabinet secretaries, the president.

"All to get us to 'yes'. She paid the most attention to vulnerable members; Pelosi knew they would pay the highest price for doing the right thing, and they did.

"When all the men in the room wanted to give up - after the nose-dive in approval ratings, media vitriol and unrelenting protests - Pelosi started counting votes and doing the kind of bare-knuckles work that was needed."

Eight months after that final vote, Democrats lost 63 seats in the House and their majority - in part because of the conservative furore over healthcare reform, and Ms Pelosi's role in pushing it through.

She had grown so politically toxic that she couldn't campaign publicly for Democrats.

Republicans, smelling blood, aired more than 150,000 television spots that year that featured her - and she has become a favourite target ever since.

It's part of the reason why, as recently as 2015, journalists were giving her "practically zero chance" of ever becoming speaker again.

They were wrong.

The lady in red

There are times in politics where the image matches the moment. Such was the case on 11 December, when Ms Pelosi walked out of the White House in a bright red coat, slipped on her sunglasses and addressed the gathered reporters. Her future, which had seemed uncertain even months earlier, was once again bright.

She, along with Senate Minority Leader Chuck Schumer, had just finished a remarkable televised Oval Office sparring match with President Trump over border security and the impending government funding crisis.

When Mr Trump implied she was not popular in her party, she shot back.

"Mr President, please don't characterise the strength that I bring to this meeting as the leader of the House Democrats, who just won a big victory."

Image copyright Alamy Image caption This photo sparked a thousand memes

Mr Trump wasn't all that wrong to suggest that Ms Pelosi was feeling some heat from members of her own party.

During the mid-term elections, more than 60 Democratic candidates campaigned on a pledge to oppose her speakership bid. After their mid-term success, a group of 16 incoming House Democrats issued a letter calling on new party leadership. They suggested it was time for the 78-year-old Pelosi to hand over power to a new generation.

In the ensuing weeks, however, the California Democrat went about slowly dismantling the rebellion. She peeled off some of the letter's signatories and sympathisers with promises of plum committee seats or prioritising their issues.

Then came her White House showdown, which set Democrats buzzing and launched a thousand complimentary memes on social media.

A day later Ms Pelosi all but secured her speakership with her biggest concession - agreeing to eight-year term limits for members of the House Democratic leadership, applied retroactively.

"Over the summer, I made it clear that I see myself as a bridge to the next generation of leaders, a recognition of my continuing responsibility to mentor and advance new members into positions of power and responsibility in the House Democratic Caucus," she said.

Ms Pelosi is a woman in a party that has swept to power in large part due to the engagement and support of women. Fifty-eight percent of those who voted for Democrats in the House were women. There will be 89 women out of the 235 Democratic members of Congress.

That's a far cry from when Ms Pelosi first entered Congress in 1988, when she was one of only 24 women in the entire 435-seat chamber.

More on this female surge

More women than ever before won seats in Congress in the 2018 mid-terms.

What does it mean for Congress - and America?

Last woman standing

So the question, then, is why has Ms Pelosi been a political survivor? She's not a compelling public speaker. Her delivery is choppy and, at times, grating. Her jokes range from mildly funny to groan-inducing.

She can, however, count votes like few in Congress - a throwback to her days as the child of Baltimore city politics. She's exceedingly organised, in both her personal and professional life.

Not far off from her 80th birthday, she is also tireless. She is up at 5:30 every morning and works late into the night. She seldom takes holidays.

Perhaps because of these efforts, she's a fund-raising powerhouse. From 2002 to 2018, the period that marked her ascent to and occupation of the highest perch in her party's House leadership, she raised $680m for Democrats.

"Speaking was never her strong suit," Povich says. "Her strong suit was the insider game, the coalition game, the organiser game."

Image copyright Getty Images Image caption Pelosi heads to the House chamber where her party swept back to power in 2018

In the days ahead, Ms Pelosi will face a formidable set of challenges.

She must balance a restive base that yearns for confrontation with a more moderate Democratic establishment keen on keeping power, after eight years without.

When asked recently about how she viewed Robert Mueller's ongoing Russia collusion investigation, her answer was an exercise in the art of the political dodge.

"From our standpoint, what we're interested in is meeting the needs of America's working families," she said.

"To spend our time lowering health-care costs by reducing the cost of prescription drugs, increasing paycheques by building infrastructure of America. Both of those things are things that the president said he wanted to do during the campaign. So this is common ground."

Democrats these days appear to have little interest in finding common ground with Mr Trump.

It's much more probable that the coming two years will be defined by acrimony, conflict and partisan gridlock.

The part played by Ms Pelosi will be crucial.

The Californian fought for eight long years to get back the gavel. It's time to see what she does with it.