Hold your nose and don’t spit out your coffee: Doctors have found a way to put healthy people’s fecal matter into pills that can cure serious gut infections — a less yucky way to do “fecal transplants.” Canadian researchers tried this on 27 patients and cured them all after strong antibiotics failed to help.

It’s a gross topic but a serious problem. Half a million Americans get Clostridium difficile, or C-diff, infections each year, and about 14,000 die. The germ causes nausea, cramping and diarrhea so bad it is often disabling. A very potent and pricey antibiotic can kill C-diff but also destroys good bacteria that live in the gut, leaving it more susceptible to future infections.

Recently, studies have shown that fecal transplants — giving infected people stool from a healthy donor — can restore that balance. But they’re given through expensive, invasive procedures like colonoscopies or throat tubes. Doctors also have tried giving the stool through enemas but the treatment doesn’t always take hold.

There even are YouTube videos on how to do a similar treatment at home via an enema. A study in a medical journal of a small number of these “do-it-yourself” cases suggests the approach is safe and effective.

Dr. Thomas Louie, an infectious disease specialist at the University of Calgary, devised a better way — a one-time treatment custom-made for each patient.

Donor stool, usually from a relative, is processed in the lab to take out food and extract the bacteria and clean it. It is packed into triple-coated gel capsules so they won’t dissolve until they reach the intestines.

“There’s no stool left — just stool bugs. These people are not eating poop,” and there are no smelly burps because the contents aren’t released until they’re well past the stomach, Louie said.

Days before starting the treatment, patients are given an antibiotic to kill the C-diff. On the morning of the treatment, they have an enema so “the new bacteria coming in have a clean slate,” Louie said.

It takes 24 to 34 capsules to fit the bacteria needed for a treatment, and patients down them in one sitting.

Louie described 27 patients treated this way on Thursday at IDWeek, an infectious diseases conference in San Francisco. All had suffered at least four C-diff infections and relapses, but none had a recurrence after taking the poop pills. Margaret Corbin, 69, a retired nurse’s aide from Calgary, told of the misery of C-diff.

“It lasted for two years. It was horrible. I thought I was dying,” she said.

With her daughter as the donor, she took pills made by Louie two years ago, and “I’ve been perfectly fine since,” Corbin said.

Dr. Curtis Donskey of the Cleveland Veterans Affairs Medical Center, who has done fecal transplants through colonoscopies, praised the work.

“The approach that Dr. Louie has is completely novel — no one else has done this,” he said. “I am optimistic that this type of preparation will make these procedures much easier for patients and for physicians.”