0

I want to store my python code result in to csv file, but here is my python code i am not show my python result in my csv file I have converted the macro vb file to python... Any advise would be more appreciated, because I am new to this.

*unable to enter full code due to site error. Please find my code

import pandas as pd
import csv
import numpy as np 
from vb2py.vbfunctions import *
from vb2py.vbdebug import *

def My_custom_MACRO():
#
# My_custom_MACRO Macro
#
#
Range('A1:A2').Select()
Range('A2').Activate()
Columns('A:A').EntireColumn.AutoFit()
Columns('G:G').Select()
Selection.Insert(Shift=xlToRight, CopyOrigin=xlFormatFromLeftOrAbove)
Columns('P:P').EntireColumn.AutoFit()
Columns('P:P').Select()
Selection.Cut(Destination=Columns('G:G'))
Range('G53').Select()
ActiveWindow.SmallScroll(Down=- 45)
Range('G1').Select()
ActiveCell.FormulaR1C1 = 'Live Deli'
ActiveWindow.SmallScroll(Down=- 9)
ActiveSheet.Range('$A$1:$N$201').AutoFilter(Field=7, Criteria1='>50', Operator=xlAnd)
ActiveSheet.Range('$A$1:$N$201').AutoFilter(Field=4, Criteria1='>50', Operator=xlAnd)
ActiveSheet.Range('$A$1:$N$201').AutoFilter(Field=7, Criteria1='>50', Operator=xlAnd)
ActiveWindow.SmallScroll(Down=0)
ActiveSheet.Range('$A$1:$N$201').AutoFilter(Field=4, Criteria1='>50%', Operator=xlAnd)

df_1=pd.read_csv(r'D:\proj\project.csv',My_custom_MACRO)
df_1.to_csv(r'D:\proj\project_output.csv') 
Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73
  • Maybe this can help you: https://stackoverflow.com/questions/3345336/save-results-to-csv-file-with-python/28170228 – QuagTeX Jul 27 '21 at 12:30
  • Is this generated with vb2py? As far as I know this tool works for visual basic and not for vba. Providing the original VBA code would be helpful. The VBA script was recorded and therefore has many unneeded steps. It insert a column at position 7 and names it "Live Deli". A filter greater than 50 or 0.5 is set to column 4 and 7. Because column 7 is empty, there is no data displayed. – Samuel Jul 27 '21 at 20:39
  • yes, code is generated with vb2py. All I need is customise excel without using macro on any pc. pls don't mind the macro code.. I just need align and sort - save as new csv file/ sheet. – Amal Joseph Jul 28 '21 at 07:47

1 Answers1

0

I don't see sort in your macro just a column move and filter. Try this ;

import pandas as pd
 
df = pd.read_csv(r'c:\temp\project.csv')
cols = df.columns
# cut column P paste column G as Live Deli
colP = df.pop(cols[14])
df.insert(14,'','')
df.insert(6,'Live Deli',colP)
# apply filter to col 4 and 7
df1 = df.loc[ (df[cols[3]] > 0.5) & (df['Live Deli'] > 50) ] 
# save
df1.to_csv(r'c:\temp\project_output.csv', index=False) 
CDP1802
  • 13,871
  • 2
  • 7
  • 17