Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python写入EXCEL单元格范围(NumPy?)_Python_Excel_Numpy - Fatal编程技术网

python写入EXCEL单元格范围(NumPy?)

python写入EXCEL单元格范围(NumPy?),python,excel,numpy,Python,Excel,Numpy,我正在尝试将用NumPy编写的矩阵写入EXCEL文件。我需要指定矩阵必须写入的单元格范围 我需要将矩阵写入EXCEL文件第4页中的单元格A4:Z512 现在,标准EXCEL文件有3张表,所以我需要先添加第4张表,然后将矩阵写入其中 在Python2.7中有这样做的方法吗?是否可以使用纯NumPy执行此操作?我没有使用NumPy,因此我不确定您是否可以操作excel文件。但是对于处理excel文件,我建议使用Win32 COM库。下面是我过去使用过的一些代码,这些代码使win32comapi更易于

我正在尝试将用NumPy编写的矩阵写入EXCEL文件。我需要指定矩阵必须写入的单元格范围

我需要将矩阵写入EXCEL文件第4页中的单元格A4:Z512

现在,标准EXCEL文件有3张表,所以我需要先添加第4张表,然后将矩阵写入其中


在Python2.7中有这样做的方法吗?是否可以使用纯NumPy执行此操作?

我没有使用NumPy,因此我不确定您是否可以操作excel文件。但是对于处理excel文件,我建议使用Win32 COM库。下面是我过去使用过的一些代码,这些代码使win32comapi更易于使用。您可以自己使用代码。希望这有帮助

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。这是一个非常好和有用的图书馆

我把你在表格4到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()

不需要。如果您真的需要Excel中的数据,请使用专用于Excel编辑的Python库。如果您已经拥有numpy中的数据,您可能会发现通过pandas将数据输出到Excel是最简单的。约翰:谢谢。我会调查的。非常感谢。回答得好!这回答了我的问题。看一看-它使Excel和Python之间的交互更加容易,并且可以在Windows和Mac上工作。它将自动处理NumPy数组,这样您就可以说类似于
Range('A1')。value=NumPy\u array
。目前还没有添加新工作表的方法,但我可以在下一版本中添加它。此外,在实现之前,您可以使用变通方法。如果你想知道细节,请告诉我。@Felix:看起来真不错。我将尝试一下,因为它看起来几乎可以做我在原始帖子中要求的所有事情。有没有一种方法可以使用easy_install安装xlwings?当然,您可以使用
easy_install
安装它,只需确保您也安装了依赖项
pywin32
。如果您还有任何问题,请告诉我!