Python Forum
Complete NEWB and openpyxl project
Thread Rating:
  • 6 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Complete NEWB and openpyxl project
#42
buran,

I hope it's ok that I keep posting my progress. I guess at this point I feel like a little kid with his mentor (even though I'm 56 year old!).

Taking it apart piece by piece worked wonders! Below I have working code that separates the groups and subgroups into workbooks/worksheets... though you really did that part for me... and I understand it a LOT better now too. I also have the font changed in all sheets, and the width of the columns changed to acceptable widths based on the reported width times a factor, dependent on the value of the reported width. Everything looks very nice!

Next I'm going to see if I can figure out how to change the name of the default "Sheet" that comes with every created workbook to "Counts", and then create a table that shows the counts for each Group/Subgroup combination.

For your review is the current working code.

Blagodarya vi, Buran, blagodarya vi, blagodarya vi, blagodarya vi !!

Joe

import os, glob, shutil, openpyxl
from pathlib import Path
from collections import defaultdict
from openpyxl.reader.excel import load_workbook
from openpyxl import Workbook
from openpyxl.compat import range
from openpyxl.utils import get_column_letter
from openpyxl.styles import Font
from datetime import datetime

startTime = datetime.now()

os.chdir("c://test")
 
mainfile = 'Example.xlsx'
wb = load_workbook(mainfile)
ws = wb.active

header, *data = ws.values

groups = defaultdict(lambda: defaultdict(list))

for row in data:
    subgroup, group = row[-1][:-1] , row[-1][-1]
    groups[group][subgroup].append(row)
    

for group, subgroups in sorted(groups.items()):
    wb = Workbook()
    wb.remove(wb.active)
    dest_filename = (f'{mainfile[:-5]} Group {group}.xlsx')

    for subgroup, rows in sorted(subgroups.items()):
        ws = wb.create_sheet(title = subgroup)
        ws.append(header)            
        for row in rows:
            ws.append(row)

        dims = {}
        for row in ws.rows:
            for cell in row:
                cell.font = Font(name = 'Ariel Narrow', sz = 10)
                if cell.value:
                    dims[cell.column] = max((dims.get(cell.column, 0), len(str(cell.value))))
                for col, value in dims.items():
                    if int(value) < 10:
                        ws.column_dimensions[col].width = int(value * 1.8)
                    else:
                        ws.column_dimensions[col].width = int(value * 1.2)

    wb.save(filename = (f'{mainfile[:-5]} Group {group}.xlsx')) 
Reply


Messages In This Thread
Complete NEWB and openpyxl project - by Netopia - Jan-09-2019, 08:06 PM
RE: Complete NEWB and openpyxl project - by buran - Jan-09-2019, 08:44 PM
RE: Complete NEWB and openpyxl project - by Netopia - Jan-09-2019, 09:43 PM
RE: Complete NEWB and openpyxl project - by buran - Jan-09-2019, 09:46 PM
RE: Complete NEWB and openpyxl project - by Netopia - Jan-09-2019, 09:53 PM
RE: Complete NEWB and openpyxl project - by buran - Jan-09-2019, 10:11 PM
RE: Complete NEWB and openpyxl project - by Netopia - Jan-09-2019, 10:14 PM
RE: Complete NEWB and openpyxl project - by Netopia - Jan-10-2019, 06:26 PM
RE: Complete NEWB and openpyxl project - by Netopia - Jan-10-2019, 07:37 PM
RE: Complete NEWB and openpyxl project - by buran - Jan-10-2019, 08:03 PM
RE: Complete NEWB and openpyxl project - by Netopia - Jan-10-2019, 08:30 PM
RE: Complete NEWB and openpyxl project - by buran - Jan-10-2019, 08:36 PM
RE: Complete NEWB and openpyxl project - by Netopia - Jan-10-2019, 08:39 PM
RE: Complete NEWB and openpyxl project - by Netopia - Jan-11-2019, 01:19 PM
RE: Complete NEWB and openpyxl project - by buran - Jan-11-2019, 01:44 PM
RE: Complete NEWB and openpyxl project - by Netopia - Jan-11-2019, 01:48 PM
RE: Complete NEWB and openpyxl project - by buran - Jan-11-2019, 01:54 PM
RE: Complete NEWB and openpyxl project - by Netopia - Jan-11-2019, 01:57 PM
RE: Complete NEWB and openpyxl project - by buran - Jan-11-2019, 02:47 PM
RE: Complete NEWB and openpyxl project - by Netopia - Jan-11-2019, 02:54 PM
RE: Complete NEWB and openpyxl project - by buran - Jan-11-2019, 03:00 PM
RE: Complete NEWB and openpyxl project - by Netopia - Jan-14-2019, 07:20 PM
RE: Complete NEWB and openpyxl project - by buran - Jan-14-2019, 07:44 PM
RE: Complete NEWB and openpyxl project - by Netopia - Jan-14-2019, 08:24 PM
RE: Complete NEWB and openpyxl project - by buran - Jan-14-2019, 08:27 PM
RE: Complete NEWB and openpyxl project - by Netopia - Jan-15-2019, 12:22 AM
RE: Complete NEWB and openpyxl project - by Netopia - Jan-15-2019, 02:56 AM
RE: Complete NEWB and openpyxl project - by buran - Jan-15-2019, 10:05 AM
RE: Complete NEWB and openpyxl project - by Netopia - Jan-15-2019, 02:14 PM
RE: Complete NEWB and openpyxl project - by buran - Jan-15-2019, 02:21 PM
RE: Complete NEWB and openpyxl project - by Netopia - Jan-16-2019, 04:36 AM
RE: Complete NEWB and openpyxl project - by buran - Jan-16-2019, 07:04 AM
RE: Complete NEWB and openpyxl project - by Netopia - Jan-16-2019, 03:29 PM
RE: Complete NEWB and openpyxl project - by buran - Jan-16-2019, 05:19 PM
RE: Complete NEWB and openpyxl project - by Netopia - Jan-17-2019, 04:59 PM
RE: Complete NEWB and openpyxl project - by buran - Jan-17-2019, 05:08 PM
RE: Complete NEWB and openpyxl project - by Netopia - Jan-17-2019, 05:33 PM
RE: Complete NEWB and openpyxl project - by buran - Jan-17-2019, 06:04 PM
RE: Complete NEWB and openpyxl project - by Netopia - Jan-17-2019, 10:19 PM
RE: Complete NEWB and openpyxl project - by Netopia - Jan-18-2019, 03:31 AM
RE: Complete NEWB and openpyxl project - by buran - Jan-18-2019, 07:07 AM
RE: Complete NEWB and openpyxl project - by Netopia - Jan-18-2019, 02:15 PM
RE: Complete NEWB and openpyxl project - by Netopia - Jan-18-2019, 08:15 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Python newb need help Fictile 1 262 Apr-02-2024, 03:28 AM
Last Post: buran
  NameError issue with daughter's newb code MrGonk 2 1,481 Sep-16-2021, 01:29 PM
Last Post: BashBedlam
  Simple newb string question Involute 2 2,273 Sep-08-2019, 12:50 AM
Last Post: Involute
  please help this newb install pygame iofhua 7 6,023 May-15-2019, 01:09 PM
Last Post: buran
  Newb question: Debugging + Linting Python in Visual Studio Code Drone4four 1 2,456 Apr-15-2019, 06:19 AM
Last Post: perfringo
  Newb question about %02d %04d bennylava 30 19,871 Mar-05-2019, 11:23 PM
Last Post: snippsat
  Pthyon 3 question (newb) bennylava 11 6,036 Feb-28-2019, 06:04 PM
Last Post: buran
  newb selfie PatM 5 3,664 Feb-19-2019, 12:20 AM
Last Post: snippsat
  Newb Question - Threading in Crons vvarrior 2 2,833 Jul-20-2018, 08:12 PM
Last Post: vvarrior
  Matt's newb question 1 MattSS102 1 2,736 Aug-28-2017, 03:27 AM
Last Post: BerlingSwe

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020