简体   繁体   English

Python从另一个工作簿xlwt附加excel表

[英]Python append excel sheet from another workbook xlwt

I have read almost all the questions posted but still can't find any solution for it.我已经阅读了几乎所有发布的问题,但仍然找不到任何解决方案。

I have wb1.xls and wb2.xls.我有 wb1.xls 和 wb2.xls。

All I want is to create wb3 with wb1.xls in sheet 1 and wb2 in sheet 2 but I can't seem to figure out .. Any help ?我想要的只是在工作表 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])

...

And I'm stuck here.... Any idea ?我被困在这里......知道吗? Note I'm working with xls not xlsx.注意我正在使用 xls 而不是 xlsx。

It would depend on the original workbooks.这将取决于原始工作簿。 Are there any formulae that need to be transferred?有什么公式需要转吗? Per cell formatting, fonts, styles, highlights, etc?每个单元格的格式、字体、样式、突出显示等? If it is just raw data it is simple enough.如果它只是原始数据,那就足够简单了。

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")

This will work as long as you are not concerned with styles and highlights ect.只要您不关心样式和亮点等,这就会起作用。

This does not handle dates as excel stores them as floats.这不会处理日期,因为 excel 将它们存储为浮点数。 If you need to handle dates you will need to parse them.如果您需要处理日期,则需要解析它们。 Consider this to help with them.考虑这个来帮助他们。

暂无
暂无

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

相关问题 如何在Python中使用XLWT附加到现有的Excel工作表 - How to append to an existing excel sheet with XLWT in Python 如何使用 python xlrd/xlwt 从一个 excel 工作簿和 output 将数据提取到另一个工作簿? - how to extract data from one excel workbook and output to another using python xlrd/xlwt? 使用Python(和DataNitro)将单元格从一个Excel工作簿中的特定工作表复制到另一个Excel工作簿中的特定工作表 - Using Python (and DataNitro) to copy cells from a particular sheet in one Excel workbook, to a particular sheet in another Excel workbook 如何使用 python 将 excel 工作簿中的内容复制到另一个工作簿而不丢失 excel 格式 - How to copy contents from a sheet of an excel workbook to another workbook without loosing the excel formatting using python 在Python中使用XLWT在Excel中附加行 - append rows in Excel using XLWT in Python 如何 append 列出 python 中现有 excel 工作簿(同一张表)的列表 - How to append a list to an existing excel workbook (same sheet) in python Python 代码不在 Excel 工作表中写入输出,但能够从同一工作簿中的另一个工作表获取输入 - Python code not writing output in excel sheet but is able to take input from another sheet in same workbook 如何使用 Python 将数据从一个 Excel 工作表复制到同一工作簿的另一工作表? - How to copy data from One Excel sheet to another sheet for same Workbook Using Python? 从XLWT以横向模式设置Excel工作表 - Setting an excel sheet in landscape mode from XLWT 如何在 Python 中将 Excel 工作表复制到另一个工作簿 - How to copy over an Excel sheet to another workbook in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM