Pacific Gas and Electric Co.’s lobbying expenses have soared as the utility giant struggles to deal with a Legislature determined to avoid a repeat of the deadly wildfires that have ravaged California.

In the first nine months of 2018, the company spent $8.35 million to lobby California legislators on a variety of bills. The number for the entire year is sure to be far higher — just how much higher will be clear Thursday, when PG&E reports its full 2018 lobbying costs.

By contrast, from 2007 to 2017, the company spent an average of $1.37 million annually on general lobbying costs, according to figures provided by California’s secretary of state.

PG&E declined to comment on the increased spending, saying that all necessary information is contained in the public lobbying reports it filed with the state.

Its spending boost hasn’t gone unnoticed.

In a San Francisco federal court hearing Wednesday, U.S. District Judge William Alsup complained that the company was spending money on lobbying at a time it is looking at huge costs to keep trees clear of its utility lines. Downed electric lines or trees coming into contact with PG&E power equipment sparked nine of the October 2017 fires that raged across the North Bay and Mendocino County, state investigators concluded.

Those fires killed 17 people. Victims and their families who have sued PG&E are among the creditors who will have to get in line for payments now that the company has filed for bankruptcy protection.

“You spend so much money on lobbying, you could go to Sacramento today and get a law passed on cutting down those trees” that can knock down power lines and spark wildfires, Alsup told PG&E lawyers.

The utility did not respond directly to the judge’s comments.

“PG&E shares the court’s commitment to safety and agrees with the urgency that we all have to work together to reduce the risk of wildfire throughout Northern and Central California,” said Lynsey Paulo, a company spokeswoman.

PG&E representatives are a common sight working the halls of the state Capitol, visiting legislators, talking to staff members and testifying at hearings. As the state’s largest utility and one of California’s biggest employers, the company is involved in a wide range of bills that come before the Legislature.

Legislation dealing with cap-and-trade regulations, clean energy goals and the consumer choice rules that allow communities like San Francisco to go outside PG&E to buy power all involve the utility, as do dozens of other consumer and workplace bills that come before the Legislature each year.

But it’s the wildfires of the past two years, and the potential liability that forced the utility to file for bankruptcy protection this week, that have sent the company’s lobbying costs into overdrive, said Democratic state Sen. Jerry Hill of San Mateo.

“There are a lot of issues they care about, but nothing but the wildfires and (PG&E’s) exposure to the costs have caused the increase in lobbying,” Hill said. “They hired every lobbying firm in Sacramento they thought had the ability to work with different legislators.”

Hill said the result was seen in August when the Legislature passed SB901, which allowed the utility to use bonds backed by its customers to pay off more than 200 lawsuits filed against the company.

The bill “was designed to give PG&E a chance for a bailout by making their customers pay for the company’s negligence,” said Hill, who was the only Democratic state senator to vote against it.

The utility also spends millions of dollars each year in contributions to the state Republican and Democratic parties and politicians. It donates to candidates for all levels of government and spends money to support or oppose any number of state propositions and local measures that might affect PG&E’s business.

In 2017-18 alone, the company gave $462,000 to the state Republican Party and $365,000 to the state Democratic Party.

PG&E also gave hundreds of contributions to individual politicians, ranging from $208,000 to back Gavin Newsom’s successful campaign for governor all the way down to $250 to re-elect Magdalena Carrasco to the San Jose City Council and $225 to put Daron McDaniel back on the Merced County Board of Supervisors.

Looking at the high-powered and high-priced lobbying, Hill said PG&E’s actions aren’t unusual.

“That’s how everyone does it in Sacramento,” he said.

Chronicle staff writer J.D. Morris contributed to this report.

John Wildermuth is a San Francisco Chronicle staff writer. Email: jwildermuth@sfchronicle.com Twitter: @jfwildermuth