2014-09-22 172 views
-1

我想写一个用NumPy编写的矩阵到EXCEL文件。我需要指定一个必须写入矩阵的单元格范围。python写入EXCEL单元格范围(NumPy?)

我需要将矩阵写入EXCEL文件的工作表4中的单元格A4:Z512。

现在,标准EXCEL文件有3张纸,所以我需要先添加第4张纸然后再写入矩阵。

有没有办法在python 2.7中做到这一点?是否可以用纯NumPy做到这一点?

+0

编号如果您真的需要在Excel中使用专用于Excel编辑的Python库, – Manhattan 2014-09-22 14:37:05

+1

如果你已经有了numpy的数据,你可能会发现通过熊猫输出到excel是最容易的。 – JohnE 2014-09-22 15:49:06

+0

JohnE:谢谢。我会仔细看看的。 – 2014-09-24 00:43:40

回答

1

我还没有使用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() 
+0

非常感谢。很好的答案!这回答了我的问题。 – 2014-09-24 00:42:37

+0

查看http://xlwings.org - 它使得Excel和Python之间的交互更容易,并且可以在Windows和Mac上运行。它会自动处理NumPy数组,所以你可以说'Range('A1')。value = numpy_array'。目前还没有方法可以添加新工作表,但是我可以在下一个版本中添加。此外,您可以使用解决方法,直到它实施。让我知道你是否想要细节。 – 2014-09-25 18:25:59

+0

@Felix:看起来非常好。我会试一试,因为它看起来可以完成原始文章中我所需要的任何事情。有没有办法用easy_install安装xlwings? – 2014-09-26 04:28:59