【问题标题】:Using Python to print the amount of space left in a network shared drive使用 Python 打印网络共享驱动器中剩余的空间量
【发布时间】:2013-09-17 23:49:33
【问题描述】:

我想打印网络共享(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 知识可以帮助我做到这一点?

非常感谢!

【问题讨论】:

  • 到目前为止,您尝试过吗?什么有效,什么无效?
  • 我已经通过解决方案回滚了您对问题的替换。请找到your solution in the revision history 并将其作为自己的答案发布。

标签: python excel pywin32 python-3.3


【解决方案1】:

已编辑以下评论

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 等。这需要对列字母进行额外的争论并找到该列中的第一个可用单元格。我认为这是一种比打开工作表时依赖特定单元格处于活动状态更安全的方法。

【讨论】:

  • 是的,这几乎就是我的想法......但我不想选择一个单元格(即 A2、B1 等)。我希望能够将数据输入到当前处于活动状态的单元格中,因为 Excel 宏将活动单元格移动到适当位置。顺便说一句,感谢克里斯的所有帮助,这真的很有帮助。
  • @user2696497 我在这里发布了更新的答案。现在,这将遍历您指定的列的单元格并找到第一个可用的列。如果您有任何问题,请告诉我。
  • 太棒了,我会在星期一早上试一试,然后告诉你进展如何。再次感谢克里斯。
  • 不得不进行一些更改(请参阅修改后的答案),但现在已排序 :) 谢谢!
  • @user2696497 很高兴你最终到达了那里。如果问题已解决,请将其标记为已接受的答案。您确定修改后的问题可以正常工作吗?这可能只是一个缩进问题,但看起来对空的更新和行增量目前在 while 循环之外,理论上这会使它陷入无限循环。
【解决方案2】:

我认为您可能需要将其分解为子问题,对每个问题进行尝试,并针对您遇到的任何问题发布问题,包括您在帖子中的尝试。以下是一些建议的步骤和领域:

  1. 计算驱动器上的剩余空间。有一个线程here 可能对此有所帮助。
  2. 要写入 Excel,有一个名为 xlwt 的模块,可以从 here 下载。我还找到了使用此here 的简短教程。

编辑

对于 Python 3.x,我找到了一个值得一试的 xlwt 端口。我发现了一个帖子 here 指向一个 github 存储库 here。我看不到安装程序,但它应该可以直接从源代码构建。

您需要从这个 github 页面下载所有内容到一个文件夹中,然后从命令行 cd 到该目录并运行

Python setup.py install

【讨论】:

  • 我尝试安装 xlwt,但没有成功。在安装时它提到它仅适用于 Python 2.2 - 2.7,但我正在运行 3.3。顺便说一句,感谢您回复我:
  • @user2696497 好的,我自己没有尝试过,但我找到了一个说它支持 3.3 的模块:xlsxwriter.readthedocs.org/en/latest
  • 谢谢,我会试一试。目前正在试用 openpyxl,但遇到了一些问题
  • 刚刚进行了检查,但 xlsxwriter 无法修改现有的电子表格(这是我需要的),它只会创建新的。
  • 有人将 xlwt 移植到 Python 3,这可能值得一试。我在这里找到了一个帖子(groups.google.com/forum/#!topic/python-excel/cnMjq-9Jp1Q),指向了一个 github 存储库(github.com/hlawrenz/xlwt/tree/py3)。我看不到安装程序,但从源代码构建应该很简单。我正在编辑上面的答案,详细说明如何执行此操作。
猜你喜欢
  • 2019-05-11
  • 2022-07-21
  • 1970-01-01
  • 1970-01-01
  • 2017-10-14
  • 2017-09-20
  • 2019-10-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多