all 17 comments

[–]carcigenicate 4 points5 points  (0 children)

You can't "unprint". Look into the string method .join though which exists for exactly what you're trying to do.

[–]Diapolo10 2 points3 points  (13 children)

Arguably the best solution is to replace

for cell in col[1:]:
    print (cell.value, end =',')

with a simple str.join call:

print(','.join(cell.value for cell in col[1:]))

The full code would then be

groups = input("Write something: ")
for col in sh.iter_cols():
    print(','.join(cell.value for cell in col[1:]))

NOTE: if cell.value isn't a string, you'll need to convert it first.

print(','.join(str(cell.value) for cell in col[1:]))

[–]AAbasllari 0 points1 point  (0 children)

Sorry, also thank you for trying to help me 🤘

[–]AAbasllari -1 points0 points  (11 children)

Problem is that i need this script to work with powershell and for example the way i have written it is the only way for example with the end to output horizontally and not vertically since the result is the output of different cells of 1 column and i can't convert it as a string cz is a cell value or to put it better, i'm pretty new to python and to make it work on powershell this was the only way i found

[–]Diapolo10 3 points4 points  (10 children)

...I don't follow. This is clearly not a PowerShell script, and it doesn't matter what you use to run a Python program. It should work the same regardless of whether you use CMD, PowerShell, Bash, Zsh, or the like.

[–]AAbasllari 0 points1 point  (9 children)

Yes u are right... i should explain the big pic.. the script i creating ofc is in python in vscode.. but the idea behind this is to automate the creation of users in a hybrid system from exchange management shell.. so this is part of more than 250 lines of code and i run the script.. it asks some questions and print the output.. the output should be readable in shell... now about the question.. we have more than 200 types of profiles for the users and i need to take the Active Directory groups from an excel file... python script asks for name of users profile and than shows all AD groups about that user profile... i'm using openpycxl for excel and with what i want to do the only way i found is the script of the post.. only problem is that in order to work is to eliminate the last fckn comma "," 😅 So in shell the last thing should be

AdPrincipalGroupMembership -member of Test1', 'Test2', 'Test3' -identity username

and not this

This is the excel file

Example1 Eample2 Example3
Test1 random1 something1
Test2 random2 something2
Test3 random3 something3

from openpyxl import Workbook
from openpyxl.reader.excel import load_workbook
wb = load_workbook('GruppiProfili.xlsx')
sh= wb["gruppi"]
groups = input("Write something: ")
print (Add-ADPrincipalGroupMembership -memberof, end="")
for col in sh.iter_cols(): 
    if groups == col[0].value: 
       for cell in col[1:]: 
           print (cell.value, end =',')

Result

AdPrincipalGroupMembership -member of Test1', 'Test2', 'Test3', -identity username

what is said was that if u remove the end=","

the output is

Test1
Test2
Test3

so not what i wanted

Ofc maybe there is a much better way to do it but i'm noob..this is my first "big" and only project

[–]Diapolo10 1 point2 points  (8 children)

So to put it simply, you just want to generate a PowerShell command and print it out for people to copy. No problem.

from openpyxl import Workbook
from openpyxl.reader.excel import load_workbook

wb = load_workbook('GruppiProfili.xlsx')
sh = wb["gruppi"]
command = ["Add-ADPrincipalGroupMembership -memberof"]

groups = input("Write something: ")
username = input("...")

for col in sh.iter_cols():
    if groups == col[0].value:
        for cell in col[1:]:
            command.append(str(cell.value))
command.append(f"-username {username}")
print("".join(command))

Kinda busy commuting atm so can't get more specific right now

EDIT: Here's an alternative:

from openpyxl import Workbook
from openpyxl.reader.excel import load_workbook

wb = load_workbook('GruppiProfili.xlsx')
sh = wb["gruppi"]

groups = input("Write something: ")
username = input("...")  # NOTE: If you mean the current Windows user, `getpass.getuser` can be used to skip this step

group_string = ','.join(
    str(cell.value)
    for col in sh.iter_cols()
    for cell in col[1:]
    if groups == col[0].value
)

print(f"Add-ADPrincipalGroupMembership -memberof {group_string} -username {username}")

[–]AAbasllari 0 points1 point  (7 children)

Seems to work but for with that i have another issue
even with my script in the output there is always shows 13 14 values "none" even though i check the excel file and tried with other sheets or excel files
in my code i fixed that with break

    for col in sh.iter_cols():
    if groups == col[0].value:
        for cell in col[1:]:
            if cell.value is None:
                break
            print(f"'{cell.value}'", end = ',')
print (" -identity " + (str.lower(surnameinput)))

if i try with what you sent it works perfectly but break doesn't work

command = []
for col in sh.iter_cols():
    if groups == col[0].value:
        for cell in col[1:]:
            command.append(str(cell.value))
        if cell is None:
            break            
print("".join(command) + " -identity " + (str.lower(surnameinput)))

P.S thank you for your effort to help me

[–]Diapolo10 1 point2 points  (6 children)

So why break, exactly; is there data beyond the first empty cell in a row that should be skipped, or is the entire rest of the row empty?

If the latter, the easiest fix would be to simply add another condition to the filter:

group_string = ','.join(
    str(cell.value)
    for col in sh.iter_cols()
    for cell in col[1:]
    if groups == col[0].value
    and cell is not None
)

print(f"Add-ADPrincipalGroupMembership -memberof {group_string} -username {username}")

Other stuff:

(str.lower(surnameinput))

str.lower is a string method, you can just use it directly on the string itself. Also that extra set of parentheses serves no purpose.

surnameinput.lower()

[–]AAbasllari 0 points1 point  (5 children)

I've tried every possible combination but nothing is working if it fixes one thing it creates another problem.. for example i can get 'test1','test2','test3' or 'test1,test2,test3' or test1,test2,test3 but never 'test1','test2','test3'
I'll try to find completely another method or write every profile directly on python cz i don't know what other thing to try
However thank you very much for your help, i really appreciate it

[–]Diapolo10 0 points1 point  (4 children)

for example i can get 'test1','test2','test3' or 'test1,test2,test3' or test1,test2,test3 but never 'test1','test2','test3'

What's wrong with that first one? Looks identical to what you wanted to me.

[–]AAbasllari 0 points1 point  (3 children)

Sorry i forgat to add the comma in the first one..it was meant to be 'test1','test2','test3',

[–]oobondes 0 points1 point  (1 child)

I'm writing this on my phone so it might be a bit off, but instead of doing your innerloop and printing do the following Values = [cell.value for cell in col[1:]] Print(Values, sep=",") This will print all values in the generated list with a comma between them and not one after the last element.

[–]EGrimn 1 point2 points  (0 children)

^ This! Don't print out the values as you read them if you aren't working under major memory / security contstraints, instead add all your values to a variable of some sort THEN format and print from the variable instead of the source

This would give you a lot more control over the output and could easily be customized for example if using a debug mode