A Go package for creating and exporting Anki decks in .apkg format.
go get github.com/ezynda3/go-anki-deck
package main
import (
"log"
anki "github.com/ezynda3/go-anki-deck"
)
func main() {
// Create a new deck
deck, err := anki.NewDeck("My Deck")
if err != nil {
log.Fatal(err)
}
defer deck.Close()
// Add cards
deck.AddCard("What is the capital of France?", "Paris")
deck.AddCard("What is 2 + 2?", "4")
// Add a card with tags
deck.AddCardWithOptions(
"What is Go?",
"A programming language",
&anki.CardOptions{
Tags: []string{"programming", "golang"},
},
)
// Save to file
err = deck.SaveToFile("output.apkg")
if err != nil {
log.Fatal(err)
}
}
// Read an image file
imageData, err := os.ReadFile("image.png")
if err != nil {
log.Fatal(err)
}
// Add media to deck
deck.AddMedia("image.png", imageData)
// Use the image in a card
deck.AddCard(
`What is this? <img src="image.png" />`,
"An example image",
)
// Method 1: Using AddAudio helper
audioData, err := os.ReadFile("pronunciation.mp3")
if err != nil {
log.Fatal(err)
}
soundTag := deck.AddAudio("pronunciation.mp3", audioData)
deck.AddCard("How do you pronounce 'hello'?", "Hello " + soundTag)
// Method 2: Using AddCardWithAudio convenience method
deck.AddCardWithAudio(
"What sound is this?",
"A bell ringing",
"bell.mp3",
audioData,
)
// Method 3: Using CardOptions for audio on both sides
deck.AddMedia("question.mp3", questionAudio)
deck.AddMedia("answer.mp3", answerAudio)
deck.AddCardWithOptions(
"Listen to the question",
"Here's the answer",
&anki.CardOptions{
Tags: []string{"audio", "listening"},
FrontAudio: "question.mp3",
BackAudio: "answer.mp3",
},
)
// Method 1: Using AddImage helper
imageData, err := os.ReadFile("diagram.png")
if err != nil {
log.Fatal(err)
}
imgTag := deck.AddImage("diagram.png", imageData)
deck.AddCard("What does this show?", "A diagram " + imgTag)
// Method 2: Using AddCardWithImage convenience method
deck.AddCardWithImage(
"Identify this structure",
"The Eiffel Tower",
"tower.jpg",
imageData,
)
// Method 3: Using CardOptions for images
deck.AddCardWithOptions(
"Compare these images",
"They show before and after",
&anki.CardOptions{
FrontImage: "before.png",
BackImage: "after.png",
},
)
// Method 1: Using AddVideo helper
videoData, err := os.ReadFile("demo.mp4")
if err != nil {
log.Fatal(err)
}
videoTag := deck.AddVideo("demo.mp4", videoData)
deck.AddCard("Watch this technique", "Explanation: " + videoTag)
// Method 2: Using AddCardWithVideo convenience method
deck.AddCardWithVideo(
"What's happening in this video?",
"A chemical reaction",
"reaction.webm",
videoData,
)
// Method 3: Multimedia card with all media types
deck.AddCardWithOptions(
"Study this content",
"Complete explanation",
&anki.CardOptions{
Tags: []string{"multimedia"},
FrontImage: "slide.png",
FrontAudio: "narration.mp3",
BackVideo: "animation.mp4",
},
)
deck, err := anki.NewDeckWithTemplate("Custom Deck", &anki.TemplateOptions{
QuestionFormat: `<div class="question">{{Front}}</div>`,
AnswerFormat: `{{FrontSide}}
<hr id="answer">
<div class="answer">{{Back}}</div>`,
CSS: `.card {
font-family: Georgia, serif;
font-size: 24px;
text-align: center;
color: #333;
background-color: #f5f5f5;
}
.question {
color: blue;
font-weight: bold;
}
.answer {
color: green;
margin-top: 20px;
}`,
})
This package supports syncing decks directly to Anki desktop using the AnkiConnect addon.
- Install Anki desktop application
- Install the AnkiConnect addon in Anki
- Ensure Anki is running when using sync features
// Create and populate a deck
deck, err := anki.NewDeck("My Deck")
if err != nil {
log.Fatal(err)
}
defer deck.Close()
deck.AddCard("Question", "Answer")
// Create AnkiConnect client
ac := anki.NewAnkiConnect()
// Push deck to Anki
err = deck.PushToAnki(ac)
if err != nil {
log.Fatal(err)
}
// Sync with options
opts := &anki.SyncOptions{
UpdateExisting: true, // Update existing cards instead of skipping
DeleteMissing: false, // Don't delete cards not in local deck
SyncMedia: true, // Sync media files (images, audio, video)
}
err := deck.SyncToAnki(ac, opts)
// Push deck with media files
err := deck.PushToAnkiWithMedia(ac, true)
// Media files are automatically detected from card content
// Supported formats:
// - Audio: [sound:filename.mp3]
// - Images: <img src="filename.png">
// - Videos: <video src="filename.mp4">
// Pull cards from Anki to local deck
err := deck.PullFromAnki(ac)
// This will:
// 1. Find all cards in the Anki deck
// 2. Clear local cards
// 3. Import cards from Anki with their tags
// Check connection
err := ac.Ping()
// Get all deck names
decks, err := ac.GetDeckNames()
// Create a deck directly
err := ac.CreateDeck("New Deck")
// Delete a deck
err := ac.DeleteDeck("Old Deck")
// Trigger sync to AnkiWeb
err := ac.Sync()
If AnkiConnect is running on a different port or host:
ac := anki.NewAnkiConnectWithURL("http://localhost:8765")
- Create Anki decks programmatically
- Add cards with front and back content
- Support for tags
- Media file support (images, audio, etc.)
- Custom card templates and CSS
- Export to .apkg format compatible with Anki
The main type representing an Anki deck.
Options for adding cards:
Tags []string
- Tags to associate with the cardFrontAudio string
- Audio filename to play on the front of the cardBackAudio string
- Audio filename to play on the back of the cardFrontImage string
- Image filename to display on the front of the cardBackImage string
- Image filename to display on the back of the cardFrontVideo string
- Video filename to display on the front of the cardBackVideo string
- Video filename to display on the back of the card
Options for customizing card templates:
QuestionFormat string
- HTML template for the question sideAnswerFormat string
- HTML template for the answer sideCSS string
- CSS styles for the cards
Client for communicating with AnkiConnect addon:
URL string
- AnkiConnect server URL (default: http://localhost:8765)Version int
- AnkiConnect API version (default: 6)
Options for deck synchronization:
UpdateExisting bool
- Update existing cardsDeleteMissing bool
- Delete cards not in local deckSyncMedia bool
- Sync media files
Creates a new deck with the default template.
Creates a new deck with a custom template.
Adds a card to the deck.
Adds a card with additional options like tags.
Adds a media file to the deck.
Adds an audio file to the deck and returns the Anki sound tag.
Adds an image file to the deck and returns the HTML img tag.
Adds a video file to the deck and returns the HTML video tag.
Adds a card with an audio file attached to the back.
Adds a card with an image file attached to the back.
Adds a card with a video file attached to the back.
Exports the deck as .apkg format and returns the data.
Exports the deck directly to a file.
Closes the deck and releases resources.
Pushes the entire deck to Anki, creating it if necessary.
Performs a more sophisticated sync with options.
Creates a new AnkiConnect client with default settings.
Creates a new AnkiConnect client with custom URL.
Checks if AnkiConnect is available.
Returns all deck names in Anki.
Creates a new deck in Anki.
Deletes a deck and all its cards.
Triggers Anki to sync with AnkiWeb.
Stores a media file in Anki's media folder with base64 encoding.
Retrieves detailed information about notes.
Pushes the deck to Anki with optional media sync.
Pulls cards from Anki deck and updates the local deck.
MIT
This package is a Go port of the JavaScript anki-apkg-export library.