CSV export with two notetypes

I got into trouble now with a CSV file that has two notetypes in each of which the tags column is in a different position. Since one notetype is with 5 fields and the other with 4.
The result is that one field is considered a tag field and the tag field becomes the fifth field.
Maybe you should add in the CSV information about the tag column by notetype to solve this.

I presume you can work around this by putting tags as the first field.

1 Like

You might want to make it the default. It can only help.
I used Anki’s standard export to create several thousand files. It would definitely make it easier for me.

@Rumo any thoughts?

1 Like

If you export one note whose notetype has 2 fields and one whose notetype has 3 fields, the first row will be padded with an empty column so that tags are contained in column 4 for both columns:

Column 1 Column 2 Column 3 Column 4
Field 1/1 Field 2/2 Tags
Field 1/3 Field 2/3 Field 3/3 Tags

I don’t understand the issue here.

If you mean it already exists, it didn’t work that way for me. In the notetype with the most fields, the last field and the tags field have been replaced.

Yes, it’s supposed to work that way and it does for me; I just checked.
Maybe there’s something specific to your setup. If you let me know your exact settings, or even provide a sample deck, I will try to reproduce it.

I converted it to csv programmatically:
But that shouldn’t make a difference since it also uses the protobuff message

import os

from PyQt6.QtWidgets import QFileDialog
from aqt import mw
from aqt.utils import showInfo, getFile, getSaveFile
from anki.collection import *
from anki.utils import tmpfile
import zipfile

def get_folder_to_convert():
    apkg_path = QFileDialog.getExistingDirectory(
    parent=mw,
    caption="Select directory",
    directory="c:\\",
    options=QFileDialog.Option.DontUseNativeDialog,
)
    if isinstance(apkg_path, str):
        current_path = apkg_path.replace("/", "\\")
        return current_path
    return None

def unzip_apkg(file):
    zip = z = zipfile.ZipFile(file)
    try:
        z.getinfo("collection.anki21")
        suffix = ".anki21"
    except KeyError:
        suffix = ".anki2"

    col = z.read(f"collection{suffix}")
    colpath = tmpfile(suffix=".anki2")
    with open(colpath, "wb") as f:
        f.write(col)
    return colpath

def export_to_csv(apkg_path):
    col_path = unzip_apkg(apkg_path)
    col = Collection(col_path)
    txt_path = apkg_path.replace(".apkg", ".txt")
    col.export_note_csv(
        out_path= txt_path,
        limit= None,
        with_html= True,
        with_tags= True,
        with_deck=False,
        with_notetype= True,
        with_guid= False)
   

def convert_to_csv():
    root_path = get_folder_to_convert()
    for (dirpath, dirnames, filenames) in os.walk(root_path):
        for filename in filenames:
            print(os.path.join(dirpath, filename))
            if str(filename).endswith("apkg"):
                export_to_csv(os.path.join(dirpath, filename))

act= mw.form.menuCol.addAction("convert to csv")
act.triggered.connect(convert_to_csv)

Works just fine for me:

If you’re still having trouble, please also provide the problematic CSV.

2 Likes

I found the problem - when importing through Python, for some reason the tags field is numbered one less than it should be.
The code below (which I increased by 1 the value of the tags field) worked as expected

def import_csv_to_col(path, _col, did, allow_dupes, matchscope):
    metadata: CsvMetadata = _col.get_csv_metadata(path, None)
    metadata.deck_id = did
    metadata.dupe_resolution = allow_dupes
    metadata.match_scope = matchscope
    metadata.tags_column = metadata.tags_column + 1
    metadata.global_tags.append("test")
    request = ImportCsvRequest(path=path, metadata=metadata)
    log = _col.import_csv(request)

    if _col == mw.col:
        mw.reset()
        return log

For some reason, I only got notified about your answer now …

Are you aware column numbers are 1-based? Did you edit the CSV by hand?
I would be surprised if the Python API yielded different results than the GUI. That would likely be a bug, so please let me know if I’m wrong.

1 Like

You can try the exaYou can try the example above. The metadata there is built-in.

Still can’t reproduce it. The metadata.tags_column = metadata.tags_column + 1 breaks the import for me, while without it tags are imported correctly.

1 Like

Is there anything else I can do to troubleshoot the problem?

It starts and ends with making the issue reproducible. Your instructions so far have been a bit vague. If you provide a CSV and code that I can simply copy and execute in the debug console, then debugging should not be the problem.

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.