只复制XLS的工作表,使用Python在新的XLS中成为新的工作表? [英] Copying only worksheet of XLS to become new worksheet in new XLS using Python?

查看:258
本文介绍了只复制XLS的工作表,使用Python在新的XLS中成为新的工作表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用下面的Python测试代码,我试图将Excel( *。xls )文件中的唯一工作表复制到一个带有一个工作表的新Excel文件中。输入电子表格如下:



 从copy import deepcopy 
从xlrd import open_workbook
从xlutils.copy导入副本作为副本
从xlwt import workbook

rb = open_workbook(rC:\Temp\test1.xls,formatting_info = True)
wb = copy(rb)
new_book = Workbook )
r_sheet = rb.sheet_by_index(0)

sheets = []
w_sheet = deepcopy(wb.get_sheet(0))
w_sheet.set_name(test1 )

for range_index in range(0,r_sheet.nrows):
for col_index in range(0,r_sheet.ncols):
cell_value = r_sheet.cell(row_index, col_index).value
print cel l_value
w_sheet.write(row_index,col_index,cell_value)

sheets.append(w_sheet)
new_book._Workbook__worksheet = sheet

new_book.save rC:\Temp\test2.xls)

如果我运行代码,下面的输出,并使用名为test1的工作表创建新的Excel文件。

  Python 2.7.10(默认,2015年5月23日,09:40:32)[MSC v.1500 32位(Intel )] on win32 
有关详细信息,请输入copyright,credits或license()。
>>> ================================ RESTART ================= ===============
>>>
Col1
Col2
Col3
a
1.0
X
b
2.0
Y
c
3.0
Z
>>>

不幸的是,输出似乎有正确数量的单元格写入它,数字单元格值写为 #VALUE!





使用Python 2.7.10,是否有一种简单的方法来从一个XLS读取工作表,然后将其作为工作表写入另一个XLS文件?



我不想简单地复制电子表格,然后重新命名新的工作表,因为一旦我可以得到这个工作我想复制在十几个电子表格中的唯一工作表成为一张与十几本工作表相同名称的工作表。

解决方案

  from xlrd导入open_workbook 
从xlutils.copy导入副本

#读取工作簿
rb = open_workbook(input.xls ,formatting_info = True)

#复制到新的工作簿
wb = copy(rb)

#重命名为'test1'作为原始帖子要求
ws = wb.get_sheet(0)
ws.name ='test1'

#保存新工作簿
wb.save(output.xls)


Using the Python test code below I am trying to copy the only worksheet in an Excel (*.xls) file into a new Excel file with one worksheet.

The input spreadsheet looks like:

from copy import deepcopy
from xlrd import open_workbook
from xlutils.copy import copy as copy
from xlwt import Workbook

rb = open_workbook(r"C:\Temp\test1.xls",formatting_info=True)
wb = copy(rb)
new_book = Workbook()
r_sheet = rb.sheet_by_index(0)

sheets = []
w_sheet = deepcopy(wb.get_sheet(0))
w_sheet.set_name("test1")

for row_index in range(0, r_sheet.nrows):
    for col_index in range(0, r_sheet.ncols):
        cell_value = r_sheet.cell(row_index, col_index).value
        print cell_value
        w_sheet.write(row_index, col_index, cell_value)

sheets.append(w_sheet)
new_book._Workbook__worksheets = sheets

new_book.save(r"C:\Temp\test2.xls")

If I run the code it shows the output below and creates the new Excel file with the worksheet named test1.

Python 2.7.10 (default, May 23 2015, 09:40:32) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>> 
Col1
Col2
Col3
a
1.0
X
b
2.0
Y
c
3.0
Z
>>> 

Unfortunately, the output, which appears to have the correct number of cells written to it has all non-numeric cell values written as #VALUE!.

Using Python 2.7.10, is there a simple way to read the worksheet from one XLS and then write it as a worksheet in another XLS file?

I do not want to simply copy the spreadsheet and then rename the worksheet in the new one because once I can get this to work I want to copy the only worksheet in each of a dozen spreadsheets to become a worksheet of the same name in a spreadsheer with a dozen worksheets.

解决方案

from xlrd import open_workbook
from xlutils.copy import copy

# Read the workbook
rb = open_workbook("input.xls", formatting_info=True)

# Copy into a new workbook
wb = copy(rb)

# Rename to 'test1' as the original post asked for 
ws = wb.get_sheet(0)
ws.name = 'test1'

# Save the new workbook
wb.save("output.xls")

这篇关于只复制XLS的工作表,使用Python在新的XLS中成为新的工作表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆