In President Trump, his mercurial partner in the White House, the House speaker, Paul D. Ryan, has deftly found a foil to deflect some of the anger that had felled the man he succeeded, John A. Boehner.

WASHINGTON — Paul D. Ryan rode to power two years ago like a hero on a white horse, a reluctant candidate for House speaker elected to heal wounds left by his predecessor, who could not tame the incessant infighting between hard-line conservatives and establishment Republicans.

In one of his first real tests, Mr. Ryan discovered last week that those old wounds can reopen fast. But in President Trump, his mercurial partner in the White House, the speaker deftly found a foil to deflect some of the anger that had felled the man he succeeded, John A. Boehner.

President Trump’s fiscal deal with Democratic leaders in Congress — which passed the House with more than a third of Republicans voting against it — infuriated House conservatives, who struck first at Mr. Ryan, but ultimately turned their ire on the Trump White House. By week’s end, the men feeling the lash were Mr. Trump’s Treasury secretary and budget director. If anything, Mr. Ryan may have emerged stronger.

“It was thrown at him,” said Representative Mark Sanford, Republican of South Carolina and a member of the hard-line House Freedom Caucus, referring to the fiscal deal. “He didn’t create it; he’s reacting to it. I think he laid out a course that was acceptable to the conference as a whole, and to conservatives as well, and he had the rug pulled out from underneath him.”

Mr. Ryan is certainly not out of the woods. Stephen K. Bannon, Mr. Trump’s former chief strategist, is publicly sniping at him, openly declaring war on Republican leaders on Capitol Hill.

“The Republican establishment is trying to nullify the 2016 election,” Mr. Bannon said in an interview with CBS’s “60 Minutes” that was to air on Sunday night. He singled out Senator Mitch McConnell, the Republican leader, and Mr. Ryan by name, saying, “They do not want Donald Trump’s populist, economic nationalist agenda to be implemented.”

The coming push to rewrite the tax code will present the speaker with his biggest challenge yet. The first major rewrite of the tax code in 30 years, an ambitious and difficult task at any time, has emerged as a must-pass measure for Mr. Ryan, its biggest champion. The failure of Republicans to repeal the Affordable Care Act, followed by the passage of a Democrat-approved fiscal and hurricane-relief package, has only amplified the pressure on Republicans to show their constituents they can govern.

“Some of us feel that we got jammed when you couple Harvey disaster aid and the debt limit,” said one outspoken conservative, Representative Dave Brat, Republican of Virginia. He added, “The leadership just needs to give us right now a tax plan.”

Representative Mark Meadows, the North Carolina…