1. 程式人生 > 其它 >用Python統計電腦資訊

用Python統計電腦資訊

# -*- coding: utf-8 -*-

import os, re, openpyxl
import time

def file_name(file_dir):
listfile = os.listdir(file_dir)
strfile = listfile.__str__()
#print(strfile)
return strfile


def regix(listname, strfile):
i=0
for name in listname:
pattern = re.compile(name)

if pattern.search(strfile) is not None:
pass
else:
i=i+1
print(name)
print(i)

def readExel():
filename = r'D:\123.xlsx'
inwb = openpyxl.load_workbook(filename) # 讀檔案
listname = []
# 獲取讀檔案中所有的sheet,通過名字的方式
for sheetnames in inwb.sheetnames:
ws = inwb[sheetnames] # 獲取第一個sheet內容

# 獲取sheet的最大行數和列數
rows = ws.max_row
cols = ws.max_column
for r in range(3, rows):
for c in range(10, 11):
if ws.cell(r, c).value is not None:
listname.append(ws.cell(r, c).value)
#print(ws.cell(r, c).value)
return listname


strfile = file_name(r"\\172.16.1.20\AcmeData\檢查報告")
listname = readExel()
regix(listname, strfile)
# def writeExcel(self):
# outwb = openpyxl.Workbook() # 開啟一個將寫的檔案
# outws = outwb.create_sheet(index=0) # 在將寫的檔案建立sheet
# for row in range(1, 70000):
# for col in range(1, 4):
# outws.cell(row, col).value = row * 2 # 寫檔案
# print(row)
# saveExcel = "D:\\work\\Excel_txtProcesss\\test.xlsx"
# outwb.save(saveExcel) # 一定要記得儲存