简体   繁体   English

python:使用xlrd存储所有单元格值,并使用xlwt写入新工作簿

[英]python: Store all cell values with xlrd and write to new workbook using xlwt

im trying to copy all cells on a sheet to a new workbook. 即时通讯试图将工作表上的所有单元格复制到新的工作簿。 i can store cell values manually like in the example code below and paste variable in respective cells but i want to automate the collection of cell data. 我可以像下面的示例代码中那样手动存储单元格值,并在各个单元格中粘贴变量,但我想自动收集单元格数据。 I am very new to python but i can conceptually see something along the line of this but i could use some help to finish it, thanks! 我对python非常陌生,但是我可以在概念上看到与此类似的东西,但是我可以使用一些帮助来完成它,谢谢!

attempt to automate cell collection 尝试自动收集细胞

 def cell(r,c): 
   set r+=1 
    cellname = c.isalpha() + r
      if r <= sheet.nrow:  
           cellname = (r,c,sheet.cell_value) 

...... i get lost around here but i assume there should be a sheet.ncols and nrows ......我在这里迷路了,但我认为应该有一张工作表。

current manual cell copying 当前的手动单元格复制

 cellA1 = sheet.cell_value(0,0)
 cellA2 = sheet.cell_value(1,0)
 cellA3 = sheet.cell_value(2,0)
 cellA4 = sheet.cell_value(3,0)
 cellA5 = sheet.cell_value(4,0)
 cellB1 = sheet.cell_value(0,1)
 cellB2 = sheet.cell_value(1,1)

workbook = xlwt.Workbook()
 sheet = workbook.add_sheet('ITEM DETAILS')

manual cell pasting 手动单元格粘贴

sheet.write(0, 0, cellA1)
sheet.write(1, 0, cellA2)

You can just simply loop through the cells in the sheet, by using sheet.nrows and sheet.ncols as the limit to loop up to. 您可以使用sheet.nrowssheet.ncols作为循环的限制,简单地循环浏览工作表中的单元格。 Also, make sure you do not define the new worksheet you are creating as sheet itself, use a new name. 另外,请确保没有将要创建的新工作表定义为工作sheet本身,而是使用新名称。 Example: 例:

newworkbook = xlwt.Workbook()
newsheet = newworkbook.add_sheet('ITEM DETAILS')
for r in range(sheet.nrows):
    for c in range(sheet.ncols):
        newsheet.write(r, c, sheet.cell_value(r, c))

Then use newsheet instead of sheet wherever you want to use the new sheet. 然后,在要使用新工作sheet任何地方使用newsheet代替工作表。

Anand S Kumar's answer is correct but you need to change i to r and j to c . Anand S Kumar的答案是正确的,但您需要将i更改为r ,将j更改为c For extra benefit I added a bit more code for a complete code example. 为了获得更多好处,我为完整的代码示例添加了更多代码。 This code opens an existing excel file, reads all of the data from the first sheet, and writes that same data to a new excel file. 此代码打开一个现有的excel文件,从第一张工作表中读取所有数据,并将相同的数据写入新的excel文件。

    import os,xlrd,xlwt

    if os.path.isfile(outExcel):os.remove(outExcel)#delete file if it exists

    inExcel= (r'C:\yourpath\inFile.xls')
    outExcel= (r'C:\yourpath\outFile.xls')

    workbook = xlrd.open_workbook(inExcel)
    sheetIn = workbook.sheet_by_index(0)

    workbook = xlwt.Workbook()
    sheetOut = workbook.add_sheet('DATA')

    for r in range(sheetIn.nrows):
        for c in range(sheetIn.ncols):
            sheetOut.write(r, c, sheetIn.cell_value(r, c))

    workbook.save(outExcel)#save the result

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM