Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 90209

How to automate my if statement to write two excel documents in openpyxl?

$
0
0

In my code I am converting a csv file into pandas then I am adjusting the data in the columns

df = df[['Lastname', 'Firstname','Company','Title','Willing_to_share','Willing_to_introduce','work_phones','Work_email','Work_Street','Work_City','Work_State','Work_Zip','Personal_Street','Personal_City','Personal_State','Personal_Zip','mobile_phones','Personal_email','Note','Note_Category']]
##print(df)
## Lastname Firstname          Company Title Willing_to_share  \
0       Doe      Jane                                           
1   Malcoun       Joe  8/28/2019 14:29                          
2   Ramirez    Morgan                                           
3     Burki     Roman                                           
4      None    Jordan                                           
5      None                                                     
6  Zachuani     Reemo                                           
7    Suarez   Geraldo     
 Willing_to_introduce work_phones              Work_email  \
0                       5678743546        j@greenbriar.com   
1                             None        ceo@nutshell.com   
2                       3338765438      mramirez@nerdy.com   
3                       5468756098           burki@bvb.com   
4                             None  jordanw45490@gmail.com   
5                             None                   ronny   
6                             None                           
7                             None   

              Work_Street      Work_City Work_State Work_Zip Personal_Street  \
0        54 George street  Ridge Springs         VA    25678                   
1     212 South Fifth Ave      Ann Arbor         MI    48103                   
2              567 one st     Birmingham         AL    45678                   
3  546 fourteen street Nw         Dallas         TX    54678                   
4                                                                              
5                                                                              
6                                                                              
7       456 yellow street                                                      

  Personal_City Personal_State Personal_Zip mobile_phones Personal_email Note  \
0                                              3245687907                       
1                                                    None                       
2                                              6780431874                       
3                                              0983457690                       
4                                                    None                       
5                                                    None                       
6                                                    None                       
7                                                    None                       

  Note_Category  
0                
1                
2                
3                
4                
5                
6                
7                

I have created the workbooks before the if statement

wb = Workbook()
ws = wb.active
wb.title = 'Contacts'
wb2 = Workbook()
ws2 = wb2.active
wb2.title = 'Contacts'

I have two issues in my code the first issue is I have tried a number of solutions but I cannot add the dataframe data in the excel sheet properly according to the if statement. The if statement sets conditions to add the pandas data rows to the accepted contacts otherwise the rows not passing the condition should be added to the rejected contacts workbooks. What am I getting is in the accepting contacts the full contacts are looped in the workbook and are added once into the rejected contacts

for column, row in df.iterrows():
  row_check = row.isna()
  if (not row_check[0] and not row_check[1]) and ((not row_check[2] and not row_check[3]) or (not row_check[2]) and (not row_check[6]) or (not row_check[16]) and (not row_check[12] and not row_check[13] and not row_check[14] and row_check[15]) or (not row_check[8] and not row_check[9] and not row_check[10] and not row_check[11] and (not row_check[7] or not row_check[17]))):
       for r in dataframe_to_rows(df, index=False, header=False):
           ws.append(r)
else:
       for r in dataframe_to_rows(df, index=False, header=False):
           ws2.append(r)

wb.save("Accepted Contacts.xlsx")
wb2.save("Rejected Contacts.xlsx")

The second issue is adding borders to each excel sheet automatically according to the length of the pandas dataframe. Right now I do it manually inside both the if and else statements.

       A3 = ws['A3'] 
       A3.border = Border(top=thin, left=thin, right=thin, bottom=thin)
       A3.alignment=Alignment(horizontal='general',
         vertical='bottom',
         text_rotation=0,
         wrap_text=True,
         shrink_to_fit=False,
         indent=0)
...
       T6 = ws['T6'] 
       T6.border = Border(top=thin, left=thin, right=thin, bottom=thin)
       T6.alignment=Alignment(horizontal='general',
         vertical='bottom',
         text_rotation=0,
         wrap_text=True,
         shrink_to_fit=False,
         indent=0)

Accepted Worksheet currentlyRejected Worksheet currently


Viewing all articles
Browse latest Browse all 90209

Trending Articles