-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxlcsv
More file actions
executable file
·52 lines (45 loc) · 1.54 KB
/
xlcsv
File metadata and controls
executable file
·52 lines (45 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#! /usr/bin/env -S uv run --script
# /// script
# requires-python = ">=3.13"
# dependencies = [
# "duckdb",
# "rich",
# "tqdm",
# ]
# ///
"""
walk $1 for xlsx files and convert them to csvs using duckdb
using duckdb's copy command. No need to create a table first.
"""
import os
from duckdb import connect
from pathlib import Path
from tqdm import tqdm # Added tqdm for progress bar
def main():
args = os.sys.argv[1:]
if not args:
args = ["."] # Default to current directory if no arguments are provided
for xlf in map(Path, args):
if xlf.is_dir():
xlsx_files = list(xlf.glob("*.xlsx"))
print(f"Processing {len(xlsx_files)} files in directory: {xlf}")
for xl in tqdm(xlsx_files, desc="Converting files"):
with connect("") as con:
try:
con.execute(
f"COPY (SELECT * FROM '{xl}') TO '{xl.with_suffix('.csv')}' (FORMAT CSV, HEADER);"
)
except Exception as e:
print(f"Error converting {xl}: {e}")
# xl.unlink()
else:
print(f"Processing file: {xlf}")
with connect("") as con:
try:
con.execute(
f"COPY (SELECT * FROM '{xlf}') TO '{xlf.with_suffix('.csv')}' (FORMAT CSV, HEADER);"
)
except Exception as e:
print(f"Error converting {xlf}: {e}")
if __name__ == "__main__":
main()