I want to print the amount of space remaining (in GB) on a network share (M: drive), and then take that value and add it to an Excel spreadsheet. I'm very new to programming and need all the help I can get really!
Thanks in advance
EDIT:
Here is what I've managed so far.
import ctypes
from win32com.client import Dispatch
import pythoncom
def drivespace():
#get space in bytes
free_bytes = ctypes.c_ulonglong(0)
ctypes.windll.kernel32.GetDiskFreeSpaceExW(ctypes.c_wchar_p(u'M:\\'), None, None ctypes.pointer(free_bytes))
#calculate space in GB
free_GB = free_bytes.value/1024/1024/1024
print(free_GB)
#input data to Excel
xl = Dispatch ('Excel.Application')
xl.visible = 0
xl.Workbooks.Add (r'C:\Location\Location1\Location2\Book1.xlsm')
xl.Run('Down1') #macro inside the workbook, just to move the cell down 1 row
#here is where I need some help... something to input the data to the active cell
#xl.Cells( ?? ACTIVE CELL HERE BUT DON'T KNOW HOW ?? ).value=(free_GB)
xl.Quit()
#release held Excel process
pythoncom.CoUninitialize()
So basically, I have everything sorted other than actually printing the data in to the active cell. Does anybody have any pywin32 knowledge that may be able to help me do this?
Thanks a bunch!
Edited following comment
import ctypes, os, pythoncom
from win32com.client import Dispatch
def drivespace(drive, xl_path, col):
#get space in bytes
free_bytes = ctypes.c_ulonglong(0)
ctypes.windll.kernel32.GetDiskFreeSpaceExW(ctypes.c_wchar_p(drive), \
None, None, ctypes.pointer(free_bytes))
#calculate space in GB
free_GB = free_bytes.value/1024/1024/1024
print(free_GB)
#input data to Excel
xl = Dispatch('Excel.Application')
xl.visible = 0
wb = xl.Workbooks.Open(xl_path)
ws = wb.Worksheets(1)
# initialise values
empty = False
row = 1
# loop until first empty cell in this column
while not empty:
val = ws.Range(col+str(row)).value
print val
if val == None:
print "Found first empty cell! Writing Value..."
ws.Range(col+str(row)).value = free_GB
empty = True
row += 1
wb.Close(True)
xl.Quit()
#release held Excel process
pythoncom.CoUninitialize()
def main():
drive = 'C:\\'
xl_path = os.path.join(os.getenv('so'),'free_space_to_excel','Book1.xlsm')
xl_column = 'A'
drivespace(drive, xl_path, xl_column)
if __name__ == '__main__':
main()
You will just need to change the values in the main procedure to set them to your drive, xl_path etc. This takes an additional arguement for a column letter and finds the first available cell in that column. I think this is a safer approach than relying on a particular cell being active when you open the sheet.