Skip to content

Instantly share code, notes, and snippets.

@lamakaha
lamakaha / gist:40333af53e9eef372a8a5a113b7a4a07
Created April 19, 2025 18:24
xlwings sys args override for decoding
# This must be at the very top of your script, before importing xlwings
import sys
import urllib.parse
# Make a copy of the original sys.argv
original_argv = sys.argv.copy()
# Process each argument to decode URL-encoded spaces
for i in range(1, len(sys.argv)):
if sys.argv[i].startswith('--'):
import xlwings as xw
import pandas as pd
import random
import tkinter as tk
from tkinter import scrolledtext
from datetime import datetime, timedelta
import traceback
import os
import sys
from concurrent.futures import ThreadPoolExecutor, as_completed
import os
import re
import pandas as pd
from collections import defaultdict
def import_files():
# Your existing code for path and file detection remains the same
path = os.path.join(wkbk.api.Path, 'source')
exts = re.compile(r'\.tsv|\.csv|\.txt|\.xlsx|\.xls|\.tzo|\.LFC|\.dat', re.IGNORECASE)
def bgr_to_rgb(bgr_value):
"""
Converts a BGR integer value (like those from VBA's Interior.Color)
into an (R, G, B) tuple.
Args:
bgr_value: An integer representing the color in 0x00BBGGRR format.
Returns:
A tuple (R, G, B) with values from 0-255.
@lamakaha
lamakaha / gist:b8f678ca7363a40c9cc55621d6682190
Created May 14, 2025 13:13
apply format from source cell
import xlwings as xw
# If you need COM constants directly for things xlwings doesn't abstract:
# from win32com.client import constants as win32c
# No longer needed: _copy_range, _paste_format, _reset_cut_copy
def apply_format_from_source_cell(target_xw_range, source_xw_cell):
"""
Applies formatting from a source xlwings cell to a target xlwings range.
Args:
Looking at your code and the results, the issue appears to be with how you're handling the border copying logic. I can see a few potential problems:
1. **Border LineStyle Check**: In your code around line 583-584, you're checking `if s_border.LineStyle != xw.constants.LineStyle.xlLineStyleNone:` but then you're setting `t_border.LineStyle = s_border.LineStyle`. However, if the source border has no line style, you might still need to explicitly set it.
2. **Border Index Mapping**: The way you're iterating through `border_indices.items()` might not be correctly mapping the border positions.
Here's what I'd suggest to fix the border copying:
```python
def set_ttl_frmt(wkbk, sh, rng, frmt):
The issue is that your code is applying border properties to all edges regardless of whether the source cell actually has borders on those edges. You need to check if each border actually exists in the source before applying it to the target.
Here's the fix:
```python
def set_ttl_frmt(wkbk, sh, rng, frmt):
try:
if wkbk is None:
wkbk = xw.Book.caller()
import pandas as pd
import re
from datetime import datetime # For type checking if NavDate is datetime
# --- Define any custom processing functions based on your old logic ---
def custom_strip_logic(value):
"""
This function needs to replicate what `lstr(strip(0))` did
. The original `lstr(strip(0))` is unclear
as it doesn't specify which column it acts upon.
import polars as pl
def rollup_alpha_pl(
column_name: str,
delimiter: str = "#"
) -> pl.Expr:
"""
Creates a Polars expression to roll up unique values in a column,
handling numeric formatting, nulls, and pre-existing delimiters.
Uses only native Polars expressions for optimal performance.
@lamakaha
lamakaha / gist:9971218f63b01024a4f430ab1358408c
Created September 30, 2025 13:18
polars csv files read
import polars as pl
from pathlib import Path
def process_csvs_simple(folder_path, db_path):
# Get files for each type
type1_files = list(Path(folder_path).glob("pattern1_*.csv"))
type2_files = list(Path(folder_path).glob("pattern2_*.csv"))
# Process Type 1 files
if type1_files: