Skip to content

Instantly share code, notes, and snippets.

@themightyoarfish
Created February 2, 2025 17:57
Show Gist options
  • Save themightyoarfish/a11155cab1e2bf930da00747349b417c to your computer and use it in GitHub Desktop.
Save themightyoarfish/a11155cab1e2bf930da00747349b417c to your computer and use it in GitHub Desktop.
from pathlib import Path
import csv
import gnupg # pip install python-gnupg
def main():
csv_lines = []
passwordstore_dir = Path.home() / ".password-store"
subdir = "…"
encrypted_files = list((passwordstore_dir / subdir).rglob("*"))
gpg = gnupg.GPG(gnupghome=Path.home() / ".gnupg", use_agent=True)
gpg.encoding = "utf-8"
for ef in encrypted_files:
if not ef.is_file():
print(f"Skipping {ef.name}")
continue
file_contents = ef.open(mode="rb").read()
decrypted_contents = gpg.decrypt(file_contents)
lines = str(decrypted_contents).split("\n")
fname = Path(ef.parts[-1]).stem
directory = ef.parts[-2]
if directory != subdir:
# several items below one site
username = fname
name = directory
else:
username = lines[1]
name = ef.stem
password = lines[0]
lines = [l for l in lines if l]
csv_line = [name, *lines[:6]]
if len(lines) > 6:
csv_line.append(";".join(lines[6:]))
else:
csv_line.extend([""] * (9 - len(csv_line)))
csv_lines.append(csv_line)
csv_lines.insert(
0,
[
"Title",
"Custom Field 1",
"Custom Field 2",
"Custom Field 3",
"*Custom Field 4",
"Custom Field 5",
"Custom Field 6",
"Custom Field 7",
"Note",
],
)
with open("pass_export_for_enpass.csv", mode="w", newline="") as csv_out:
csv_writer = csv.writer(
csv_out, dialect="excel", quotechar='"', quoting=csv.QUOTE_ALL
)
for row in csv_lines:
csv_writer.writerow(row)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment