Skip to content

Commit

Permalink
XLSX - Adjust initial width for columns based on values
Browse files Browse the repository at this point in the history
  • Loading branch information
ccmars committed Feb 18, 2024
1 parent 922caeb commit 9dbb48e
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 0 deletions.
Binary file modified dist/xlsx/world_countries.xlsx
Binary file not shown.
Binary file modified dist/xlsx/world_currencies.xlsx
Binary file not shown.
Binary file modified dist/xlsx/world_languages.xlsx
Binary file not shown.
11 changes: 11 additions & 0 deletions src/scripts/csv2xlsx.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@ def csv2xlsx(csv_file_path, xlsx_file_path, sheet_name):

book = load_workbook(xlsx_file_path)
book.properties.creator = "github.com/ccmars/world-data"

worksheet = book[sheet_name]
for column in df:
max_length = 0
column = df[column]
if column.dtype != 'object':
continue
max_length = max(column.astype(str).map(len).max(), len(column.name))
col_idx = df.columns.get_loc(column.name)
worksheet.column_dimensions[chr(col_idx + 65)].width = max_length

book.save(xlsx_file_path)

csv2xlsx(
Expand Down

0 comments on commit 9dbb48e

Please sign in to comment.