# Import CSV files programmatically

**URL:** <https://forums.ankiweb.net/t/import-csv-files-programmatically/36498>\
**Category:** Development\
**Created:** [November 1, 2023, 9:38pm UTC](https://forums.ankiweb.net/t/import-csv-files-programmatically/36498 "2023-11-01T21:38:16Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![MIZMU](https://avatars.discourse-cdn.com/v4/letter/m/7993a0/32.png) [@MIZMU](https://forums.ankiweb.net/u/MIZMU)\
**Post date:** [November 1, 2023, 9:38pm UTC](https://forums.ankiweb.net/t/import-csv-files-programmatically/36498/1 "2023-11-01T21:38:16Z")

</div>

I am trying to automatically import CSV files.  
For this purpose, I am trying to use Anki’s built-in code as below.  
But it causes an error.  
What did I do wrong?

```auto
import anki
from anki.collection import ImportCsvRequest, ImportLogWithChanges
from anki.hooks import wrap
from aqt import gui_hooks, mw
from aqt.utils import getFile, showText

def import_csv_(self, request: ImportCsvRequest) -> ImportLogWithChanges:
    print(str("request"))
    log = self._backend.import_csv_raw(request.SerializeToString())
    return ImportLogWithChanges.FromString(log)

anki.collection.Collection.import_csv = wrap(anki.collection.Collection.import_csv, import_csv_)

def print_pb(*args):
    path= getFile(mw, "133", None)
    meta = mw.col.get_csv_metadata(path, None)
    request = ImportCsvRequest()
    request.path = path
    # request.metadata =meta
    log = mw.col.import_csv(request)
    showText(str(log))
print_pb()

```

error:

> anki.errors.InvalidInput: deck oneof not set

---

<div class="post-metadata">

**Author:** ![abdo](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/abdo/32/654_2.png) [@abdo](https://forums.ankiweb.net/u/abdo)\
**Post date:** [November 1, 2023, 11:21pm UTC](https://forums.ankiweb.net/t/import-csv-files-programmatically/36498/2 "2023-11-01T23:21:31Z")

</div>

You have to pass ImportCsvRequest’s fields to the constructor. This works for me:

```python
def print_pb(*args):
    path = getFile(mw, "133", None)
    metadata = mw.col.get_csv_metadata(path, None)
    request = ImportCsvRequest(path=path, metadata=metadata)
    log = mw.col.import_csv(request)
    showText(str(log))

```

---

<div class="post-metadata">

**Author:** ![MIZMU](https://avatars.discourse-cdn.com/v4/letter/m/7993a0/32.png) [@MIZMU](https://forums.ankiweb.net/u/MIZMU)\
**Post date:** [November 4, 2023, 8:38pm UTC](https://forums.ankiweb.net/t/import-csv-files-programmatically/36498/3 "2023-11-04T20:38:42Z")

</div>

It works!  
But only if I import the CSV file into my current collection.  
I tried importing it into a temporary collection that I created programmatically and nothing changes. Unlike adding a new note, it does work. Only the CSV import does not.  
(I’m trying to import a CSV file and control the deck and the notetype to which each note will be imported. Since I have no way to control this through Anki’s new import using a addon, I create a temporary collection as above and then import from it)

```auto
from anki.collection import ImportCsvRequest, ImportLogWithChanges, Collection
from aqt.utils import getFile, showText
from anki.utils import tmpdir
from aqt import mw
import os

short_col_name ="temp_for_import"
name = tmpdir() + "\\" + f"{short_col_name}"
colpath = name + "\collection.anki2"
try:
    os.mkdir(name)
except FileExistsError:
    pass
mw.temp_col = Collection(colpath)

model = mw.temp_col.models.current()
note = mw.temp_col.new_note(model)
mw.temp_col.add_note(note, mw.col.decks.selected())
path = getFile(mw, "getFile", None)
metadata = mw.temp_col.get_csv_metadata(path, None)
request = ImportCsvRequest(path=path, metadata=metadata)
print(len(mw.temp_col.find_notes("")))

```

---

<div class="post-metadata">

**Author:** ![abdo](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/abdo/32/654_2.png) [@abdo](https://forums.ankiweb.net/u/abdo)\
**Post date:** [November 5, 2023, 12:49am UTC](https://forums.ankiweb.net/t/import-csv-files-programmatically/36498/4 "2023-11-05T00:49:54Z")

</div>

Your code is missing this line: `mw.temp_col.import_csv(request)`

---

<div class="post-metadata">

**Author:** ![MIZMU](https://avatars.discourse-cdn.com/v4/letter/m/7993a0/32.png) [@MIZMU](https://forums.ankiweb.net/u/MIZMU)\
**Post date:** [November 5, 2023, 10:34pm UTC](https://forums.ankiweb.net/t/import-csv-files-programmatically/36498/5 "2023-11-05T22:34:08Z")

</div>

> [@abdo](#):
>
> mw.temp\_col.import\_csv(request)

still not working.  
(Before, I tried with this line. I forgot to add it in the example 😥.)

 ![image](https://us1.discourse-cdn.com/flex002/uploads/anki2/original/3X/c/2/c2e76fca43488cdb881c7cb7a8a732d188e43300.png)

---

<div class="post-metadata">

**Author:** ![abdo](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/abdo/32/654_2.png) [@abdo](https://forums.ankiweb.net/u/abdo)\
**Post date:** [November 8, 2023, 1:06pm UTC](https://forums.ankiweb.net/t/import-csv-files-programmatically/36498/6 "2023-11-08T13:06:22Z")

</div>

len() is returning 1 so it must have imported something? I just copied your code and run it. Not sure why that’s not working for you.

---

<div class="post-metadata">

**Author:** ![MIZMU](https://avatars.discourse-cdn.com/v4/letter/m/7993a0/32.png) [@MIZMU](https://forums.ankiweb.net/u/MIZMU)\
**Post date:** [November 8, 2023, 8:00pm UTC](https://forums.ankiweb.net/t/import-csv-files-programmatically/36498/7 "2023-11-08T20:00:57Z")

</div>

I tried again now and it worked.  
I will try to see when there is a problem.  
Thanks for the help! 👍👍👍👍

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex002/uploads/anki2/original/1X/8f1279ababc5879d54e4838989f606cfe55af8c7.jpeg) [@system](https://forums.ankiweb.net/u/system)\
**Post date:** [December 8, 2023, 8:01pm UTC](https://forums.ankiweb.net/t/import-csv-files-programmatically/36498/8 "2023-12-08T20:01:37Z")

</div>

Closed on December 8, 2023, 8:01pm UTC

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