Skip to content

Instantly share code, notes, and snippets.

@mturilin
Created January 26, 2020 07:44
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mturilin/699a50d0433010708350b8befe4f97a2 to your computer and use it in GitHub Desktop.
Save mturilin/699a50d0433010708350b8befe4f97a2 to your computer and use it in GitHub Desktop.
Converst TextExpander csv to espanso yaml
#!/usr/bin/python3
import yaml
import sys
import csv
# create root yaml
matches = []
# open file
filename = sys.argv[1]
with open(filename, newline='') as csvfile:
csv_reader = csv.reader(csvfile, delimiter=',', quotechar='"')
for row in csv_reader:
matches.append({
'trigger': row[0],
'replace': row[1]
})
# dump results into a file
espanso_root = {
'parent': 'default',
'matches': matches
}
new_filename = filename[:-4]+".yml"
dump = yaml.dump(espanso_root, encoding='utf-8', allow_unicode=True)
print(dump)
with open(new_filename,'wb') as new_file:
new_file.write(dump)
print(F"Created {new_filename}")
@Kmfernan5
Copy link

Kmfernan5 commented Apr 20, 2022

How do you run this? Im getting an error? @mturilin

PS C:\Users***> python
Python 3.10.1 (tags/v3.10.1:2cd268a, Dec 6 2021, 19:10:37) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.

convert.py
Traceback (most recent call last):
File "", line 1, in
NameError: name 'convert' is not defined

@ernstki
Copy link

ernstki commented Dec 20, 2022

@Kmfernan5 Try

python DRIVE:\PATH\TO\convert.py DRIVE:\PATH\TO\YOUR_TEXTEXPANDER_EXPORT_FILE.CSV

Then, assuming it works, look for a YOUR_TEXTEXPANDER_EXPORT_FILE.yml file in the same folder as the CSV export.

If you have trouble with the DRIVE:\ parts, it helps to drag and drop the filename from the Windows file manager into the PowerShell window. The same trick will work on other platforms, e.g., macOS and Linux.

To be clear, I have not tested this script personally—on Windows, or any other platform—I just recognized the error that you made trying to run the script with Python. You need to supply the name of the script to the python program, and then the pathname of the CSV export from TextExpander as an additional argument.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment