我想寫一個用NumPy編寫的矩陣到EXCEL文件。我需要指定一個必須寫入矩陣的單元格範圍。python寫入EXCEL單元格範圍(NumPy?)
我需要將矩陣寫入EXCEL文件的工作表4中的單元格A4:Z512。
現在,標準EXCEL文件有3張紙,所以我需要先添加第4張紙然後再寫入矩陣。
有沒有辦法在python 2.7中做到這一點?是否可以用純NumPy做到這一點?
我想寫一個用NumPy編寫的矩陣到EXCEL文件。我需要指定一個必須寫入矩陣的單元格範圍。python寫入EXCEL單元格範圍(NumPy?)
我需要將矩陣寫入EXCEL文件的工作表4中的單元格A4:Z512。
現在,標準EXCEL文件有3張紙,所以我需要先添加第4張紙然後再寫入矩陣。
有沒有辦法在python 2.7中做到這一點?是否可以用純NumPy做到這一點?
我還沒有使用NumPy,所以我不確定你是否可以操縱excel文件。但爲了處理excel文件,我推薦使用win32com庫。以下是我過去使用win32com API的一些代碼。隨意使用自己的代碼。希望這可以幫助!
import win32com.client as win32
excel = win32.gencache.EnsureDispatch('Excel.Application')
def openExcel(makeExcelVisible=True):
excel.Visible = makeExcelVisible
def closeExcel():
excel.Application.Quit()
class ExcelFile(object):
# opens up a workbook to work on, not selecting a file name creates a new one
def __init__(self, fileName=None):
if fileName == None:
self.wb = excel.Workbooks.Add()
else:
self.wb = excel.Workbooks.Open(fileName)
self.ws = None
def addWorksheet(self):
# adds a new worksheet to the workbook and makes it the current worksheet
self.ws = self.wb.Worksheets.Add()
def selectWorksheet(self, worksheetName):
# selects a worksheet to work on
self.ws = self.wb.Worksheets(worksheetName)
def renameWorksheet(self, worksheetName):
# renames current worksheet
self.ws.Name = worksheetName
def save(self):
# saves the workbook
self.wb.Save()
def saveAs(self, fileName):
# saves the workbook with the file name
self.wb.SaveAs(fileName)
def close(self):
# closes the workbook
self.wb.Close()
def insertIntoCell(self, cellRow, cellCol, data):
self.ws.Cells(cellRow,cellCol).Value = data
def clearCell(self, cellRow, cellCol):
self.ws.Cells(cellRow,cellCol).Value = None
以下是如何使用上述代碼的示例。它創建一個新的excel文件,重命名工作表,將信息添加到每個工作表的第一個單元格中,並將該文件保存爲「test.xlsx」。默認保存位置是您的主目錄。
worksheets = ["Servers", "Printers", "Drives", "IT Phones"]
information = ["Server WS", "Printer WS", "Driver WS", "IT Phone WS"]
def changeCells(information):
excelFile = ExcelFile()
for index in xrange(len(worksheets)):
sheetNumber = index + 1
if sheetNumber == 4:
excelFile.addWorksheet()
excelFile.selectWorksheet("Sheet%d" % sheetNumber)
excelFile.renameWorksheet(worksheets[index])
excelFile.insertIntoCell(1,1,information[index])
excelFile.saveAs("test.xlsx")
excelFile.close()
openExcel()
changeCells(information)
closeExcel()
此外,我建議您自己查看win32com的API。這是一個非常好的和有用的圖書館。
我把在Sheet4上輸入矩陣的實際代碼放到A4上:Z512。
def addMatrix(matrix):
# use ExcelFile("fileName.xlsx") if you need to add to a specific file
excelFile = ExcelFile()
excelFile.addWorksheet()
# default excel files only have 3 sheets so had to add one
excelFile.selectWorksheet("Sheet4")
# xrange(4,513) since end index is exclusive
for row in xrange(4,513):
# 1 for A, 26 for Z
for col in xrange(1,27):
mRow = row - 4
mCol = col - 1
excelFile.insertIntoCell(row, col, matrix[mRow][mCol])
excelFile.saveAs("test.xlsx")
excelFile.close()
matrix = list()
for row in xrange(509):
matrix.append([])
for col in xrange(26):
matrix[row].append(0)
# the matrix is now filled with zeros
# use openExcel(False) to run faster, it won't open a window while running
openExcel()
addMatrix(matrix)
closeExcel()
非常感謝。很好的答案!這回答了我的問題。 – 2014-09-24 00:42:37
查看http://xlwings.org - 它使得Excel和Python之間的交互更容易,並且可以在Windows和Mac上運行。它會自動處理NumPy數組,所以你可以說'Range('A1')。value = numpy_array'。目前還沒有方法可以添加新工作表,但是我可以在下一個版本中添加。此外,您可以使用解決方法,直到它實施。讓我知道你是否想要細節。 – 2014-09-25 18:25:59
@Felix:看起來非常好。我會試一試,因爲它看起來可以完成原始文章中我所需要的任何事情。有沒有辦法用easy_install安裝xlwings? – 2014-09-26 04:28:59
編號如果您真的需要在Excel中使用專用於Excel編輯的Python庫, – Manhattan 2014-09-22 14:37:05
如果你已經有了numpy的數據,你可能會發現通過熊貓輸出到excel是最容易的。 – JohnE 2014-09-22 15:49:06
JohnE:謝謝。我會仔細看看的。 – 2014-09-24 00:43:40