我想打印网络共享(M:驱动器)上的剩余空间(以GB为单位),然后采用该值并将其添加到Excel电子表格中。我对编程非常陌生,需要我能真正获得的所有帮助!
提前致谢
编辑:
到目前为止,这是我已经完成的工作。
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()
因此,基本上,除了将数据实际打印到活动单元中之外,我还进行了其他所有排序。有人有任何pywin32知识可以帮助我做到这一点吗?
谢谢一堆!
编辑后的评论
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()
您只需要在主过程中更改值即可将其设置为驱动器,xl_path等。这需要对列字母进行额外的争论,然后找到该列中的第一个可用单元格。我认为这比在打开工作表时依靠特定单元格处于活动状态更为安全。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句