Jan-15-2019, 12:22 AM
Thank you for that explanation... I ended up needing to know that! :)
I've gotten stuck again. I'm managed to create spreadsheets named as a concatenation of the original file and the group. I've managed to take the "subgroup" (everything before the final digit in the reorder field) and create sheets named for them in each workbook. At least a small sense of accomplishment! I was able to reuse your code to figure out how to get where I am.
Now I can't seem to figure out how to append the header and then the rows of group data to the the sheets. Here's what I've got, but I get an output message that says:
I've gotten stuck again. I'm managed to create spreadsheets named as a concatenation of the original file and the group. I've managed to take the "subgroup" (everything before the final digit in the reorder field) and create sheets named for them in each workbook. At least a small sense of accomplishment! I was able to reuse your code to figure out how to get where I am.
Now I can't seem to figure out how to append the header and then the rows of group data to the the sheets. Here's what I've got, but I get an output message that says:
Output: RESTART: C:\Users\Joe\Dropbox\Python\Misc py files\group-sort examples from buran.py
OPTION 1 output
Traceback (most recent call last):
File "C:\Users\Joe\Dropbox\Python\Misc py files\group-sort examples from buran.py", line 47, in <module>
ws.cell(row=1, column=1).value = header
File "C:\Python3x\lib\site-packages\openpyxl\cell\cell.py", line 294, in value
self._bind_value(value)
File "C:\Python3x\lib\site-packages\openpyxl\cell\cell.py", line 207, in _bind_value
raise ValueError("Cannot convert {0!r} to Excel".format(value))
ValueError: Cannot convert ('Cust#', 'First Name', 'Last Name', 'Full Name', 'Reorder') to Excel
From this code:import os, glob, shutil, openpyxl 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 os.chdir("c://test") mainfile = 'Example.xlsx' wb = load_workbook(mainfile) ws = wb.active # read header and all data in two variables -> header and data # Note the asteriks in frot of data # This way data will hold everything except the first element in ws.values header, *data = ws.values # OPTION 1 print('\n\nOPTION 1 output') # parse data into groups groups = defaultdict(list) for row in data: group = row[-1][-1] groups[group].append(row) ###print each group in desired order ##for group, rows in sorted(groups.items()): ## print('\n\nGroup {}\n---------------\n'.format(group)) ## for row in sorted(rows, key=lambda x: x[-1]): ## print(row) for group, rows in sorted(groups.items()): wb = Workbook() wb.remove(wb.active) dest_filename = (mainfile[:-5] + " Group " + group + ".xlsx") for row in sorted(rows, key=lambda x: x[-1][:-1]): subgroup = row[-1][:-1] wb.create_sheet(title = subgroup) ws.cell(row=1, column=1).value = header wb.save(filename = dest_filename)Thoughts?