简体   繁体   English

使用xlrd,xlwt和xlutils编辑现有的Excel工作簿

[英]editing existing excel workbook using xlrd, xlwt and xlutils

How to edit and save the data in an existing excel workbook using xlrd, xlwt and xlutils module? 如何使用xlrd,xlwt和xlutils模块在现有的Excel工作簿中编辑和保存数据? could someone please provide a sample code to edit and save the data in excel workbook? 有人可以提供示例代码来编辑数据并将其保存在excel工作簿中吗?

I am trying to put data from one workbook to another. 我正在尝试将数据从一个工作簿放到另一个工作簿。

import xlrd, xlwt, xlutils

wb1 = xlrd.open_workbook('workbook1.xls', formatting_info=True)
wb2 = xlrd.open_workbook('workbook2.xls', formatting_info=True)

value 1 == wb2.sheet_by_name('Sheet1).cell(2,1).value

wb1.sheet_by_name('Sheet1').cell(2,2).value == value1

How to save this data in workbook1.xls? 如何将这些数据保存在workbook1.xls中?

Sorry, I asked this before, but I am trying to be more clear about my question this time. 抱歉,我之前曾问过这个问题,但是这次我想更清楚地了解我的问题。

Thank you very much. 非常感谢你。

You can save with wb1.save('workbook1.xls') . 您可以使用wb1.save('workbook1.xls')保存。 You might get an IOError that the file already exists. 您可能会得到一个IOError文件已存在。 In that case try to os.remove() the file before saving. 在这种情况下,请尝试在保存之前先os.remove()文件。

I agree with the previous answer of using the xlwt library save method. 我同意使用xlwt库保存方法的先前答案。 But you should also do some proof reading of your code. 但是,您还应该对代码进行一些校对。 You are missing a closing quote for Sheet1 and variable names cannot have spaces. 您缺少Sheet1的右引号,并且变量名不能有空格。

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

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