【问题标题】:Python append excel sheet from another workbook xlwtPython从另一个工作簿xlwt附加excel表
【发布时间】:2016-05-03 00:03:34
【问题描述】:

我已经阅读了几乎所有发布的问题,但仍然找不到任何解决方案。

我有 wb1.xls 和 wb2.xls。

我想要的只是在工作表 1 中使用 wb1.xls 创建 wb3,在工作表 2 中使用 wb2 创建 wb3,但我似乎无法弄清楚.. 有什么帮助吗?

import xlwt
import xlrd
import glob, os
import numpy as np
from xlutils.copy import copy

os.chdir("E:/docs/")

wb1=[file for file in glob.glob("wb1*")]
wb2=[file for file in glob.glob("wb2*")]

s1 = xlrd.open_workbook(filename = wb1[0])
s2 = xlrd.open_workbook(filename = wb2[0])

...

我被困在这里....有什么想法吗?注意我使用的是 xls 而不是 xlsx。

【问题讨论】:

    标签: python xlrd xlwt


    【解决方案1】:

    这将取决于原始工作簿。是否有任何公式需要转移?每个单元格格式、字体、样式、突出显示等?如果只是原始数据,那就很简单了。

    import xlrd
    import xlwt
    
    # open first excel file, store number of rows,cols and sheet name
    wb_1 = open_workbook("file1.xls")
    sheet_1 = wb.sheet_by_index(0)
    maxRows_1 = sheet_1.nrows
    maxCols_1 = sheet_1.ncols
    sName_1 = sheet_1.name
    
    i = 0
    j = 0
    
    # create output excel file
    wb_out = xlwt.Workbook()
    sheet_out_1 = wb_out.add_sheet(sName_1)
    
    # Loop through writing each cell value
    while i<maxRows_1:
        while j<maxCols_1:
            sheet_out_1.write(i,j, sheet_1.cell(i,j).value)
            j += 1
        j = 0
        i += 1
    
    # repeat for second excel file
    # then save your new excel 
    
    wb_out.save("newFile.xls")
    

    只要您不关心样式和亮点等,这将起作用。

    这不处理日期,因为 excel 将它们存储为浮点数。如果您需要处理日期,则需要解析它们。考虑this 来帮助他们。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-23
      • 2021-06-16
      • 1970-01-01
      • 1970-01-01
      • 2017-09-18
      • 2011-03-14
      • 1970-01-01
      相关资源
      最近更新 更多