The long slide in German factory output continued in July, increasing the risk that the eurozone’s largest economy is falling into a second straight quarter of contraction.

German factories have been faltering since 2018, but suffered a fresh blow when energy costs surged in the wake of Russia’s invasion of Ukraine in early 2022. Despite some hopeful signs at the start of this year, they have yet to embark on a sustained recovery.

The sector’s weakness partly reflects anemic global demand for goods following a surge in purchases during the Covid-19 pandemic, and intense competition from Chinese producers that are sometimes supported by government subsidies . But it has also been held back by high interest rates, which have damped demand from households and businesses.

Production in the manufacturing sector fell back by 2.4% on month in July, according to adjusted figures set out Friday by the German statistics authority. That was a notably sharper decline than expected by economists, and reverses some of the increase booked a month earlier. That looks to have been an anomaly caused by ups and downs in automobile production.

“Bottoming-out of industry still has a long way to go,” said Carsten Brzeski, macro chief at bank ING, in a note to clients. “The risk of yet another quarter of stagnation or even contraction has clearly increased.”

Whatever the monthly swings, German factory output has been on yearslong downward path, and now stands more than 16% below its level at the end of 2017. Recent surveys suggest that is not about to change, with a survey of purchasing managers pointing to another drop in August. There are some signs of resilience, with figures released Thursday recording a 2.9% rise in new orders during July. However, that was driven by a number of large-scale orders for transport equipment, while new orders for consumer goods were down 5.8%.

The slump extends to Germany’s storied automobile makers, with Volkswagen on Monday suggesting that it may close a plant in the country for the first time ever. While the company’s management has pointed to weak demand in Europe as its key problem, analysts say the transition to manufacturing electric vehicles has proven challenging.

But other sectors are also hurting, including the chemical industry that was at the forefront of Germany’s nineteenth-century industrialization, and bore the brunt of a surge in energy costs in 2022.

Matthias Blum, foreign-trade chief at German chemicals-industry lobby group VCI, told The Wall Street Journal that increasing overproduction in China, as well as high energy prices and bureaucracy, are hurting the sector. Output isn’t likely to recover to prepandemic levels anytime soon, Blum warned.

The chemicals industry is Germany’s third largest after autos and machinery, and has lost a fifth of its production volume since 2018. For the first time in 2022, Germany imported more chemicals from China than it exported to that country.

“Germany is facing de-industrialization, with the chemical industry at the forefront,” warned Michael Weiss, partner at PwC consultancy business Strategy&, in a report published earlier this year.

Tougher competition from China has made it more difficult for German manufacturers to find overseas buyers. In a recent report, the European Central Bank estimated that between 2019 and 2023, the cost of making a car in Europe rose by 7.5% relative to a Chinese-made alternative, resulting in a 15% decline in global market share for European manufacturers.

But that loss of competitiveness is not confined to automobiles, and is partly the result of Chinese government support for its producers.

“China’s advanced manufacturing sectors are gaining a significant cost advantage due to substantial government subsidies, in particular in high-tech sectors,” the ECB said.

The slump in German factories spells big trouble for the economy as a whole. Manufacturing is much more important to Germany than many of its peers: it accounts for almost twice as large a share of total activity as neighboring France or the U.S. And its troubles help explain why Germany’s economy contracted in 2023, and according to new forecasts from the Kiel Institute for the World Economy, will do so again this year.

“The old macro business model of cheap energy and easily accessible large export markets is no longer working,” ING’s Brzeski said.

Germany’s economy is also suffering from cuts in government spending required to comply with the country’s self-imposed deficit limits. Output fell in the three months through June, and surveys suggest it is on course for a further decline in the current quarter.

The manufacturing sector in France, the second-largest in the eurozone, similarly fell back more than expected in July. With its largest members faltering, the eurozone economy has struggled to regain momentum following the twin blows of a surge in energy and food prices, and a rise in interest rates to tame that inflation surge. The European Central Bank cut its key interest rate in June, and investors expect it to do so again next week, but more support may be needed if a sharp slowdown is to be avoided.

“We must ensure that inflation converges to our target without holding back the economy unnecessarily, because we desperately need investment and growth in Europe,” said Piero Cipollone, a member of the ECB’s executive board, in an interview with French newspaper Le Monde.

Write to Joshua Kirby at  joshua.kirby@wsj.com ; @joshualeokirby and to Paul Hannon at  paul.hannon@wsj.com