简体   繁体   English

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

[英]Edit existing excel workbooks and sheets with xlrd and xlwt

In the documentation for xlrd and xlwt I have learned the following: xlrdxlwt文档中 ,我学到了以下内容:

How to read from existing work-books/sheets: 如何阅读现有的工作簿/表格:

from xlrd import open_workbook
wb = open_workbook("ex.xls")
s = wb.sheet_by_index(0)
print s.cell(0,0).value
#Prints contents of cell at location a1 in the first sheet in the document called ex.xls

How to create new work-books/sheets: 如何创建新的工作簿/表:

from xlwt import Workbook
wb = Workbook()
Sheet1 = wb.add_sheet('Sheet1')
Sheet1.write(0,0,'Hello')
wb.save('ex.xls')
#Creates a document called ex.xls with a worksheet called "Sheet1" and writes "Hello" to the cell located at a1

What I want to do now is to open an existing worksheet, in an existing workbook and write to that sheet. 我现在要做的是在现有工作簿中打开现有工作表并写入该工作表。

I have tried something like: 我尝试过类似的东西:

from xlwt import open_workbook
wb = open_workbook("ex.xls")
s = wb.sheet_by_index(0)
print s.cell(0,0).value

but open_workbook is only part of the xlrd module, not xlwt . 但是open_workbook只是xlrd模块的一部分,而不是xlwt

Any ideas? 有任何想法吗?

Edit1: After Olivers suggestion I looked into xlutils and tried the following: Edit1:在Olivers建议之后,我调查了xlutils并尝试了以下方法:

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

wb = open_workbook("names.xls")
s = wb.get_sheet(0)
s.write(0,0,'A1')
wb.save('names.xls')

This however, gives me the following error message: 但是,这给了我以下错误消息:

File "C:\Python27\lib\site-packages\xlrd\book.py", line 655, in get_sheet
raise XLRDError("Can't load sheets after releasing resources.")
xlrd.biffh.XLRDError: Can't load sheets after releasing resources.

Edit 2: The error message was due to improper use of the get_sheet function. 编辑2:错误消息是由于不正确使用get_sheet函数。 Finally found out how to use it: 终于找到了如何使用它:

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

rb = open_workbook("names.xls")
wb = copy(rb)

s = wb.get_sheet(0)
s.write(0,0,'A1')
wb.save('names.xls')

As I wrote in the edits of the op, to edit existing excel documents you must use the xlutils module (Thanks Oliver) 正如我在op的编辑中所写,要编辑现有的Excel文档,您必须使用xlutils模块(Thanks Oliver)

Here is the proper way to do it: 这是正确的方法:

#xlrd, xlutils and xlwt modules need to be installed.  
#Can be done via pip install <module>
from xlrd import open_workbook
from xlutils.copy import copy

rb = open_workbook("names.xls")
wb = copy(rb)

s = wb.get_sheet(0)
s.write(0,0,'A1')
wb.save('names.xls')

This replaces the contents of the cell located at a1 in the first sheet of "names.xls" with the text "a1", and then saves the document. 这将使用文本“a1”替换第一张“names.xls”中位于a1的单元格的内容,然后保存文档。

Here's another way of doing the code above using the openpyxl module that's compatible with xlsx. 这是使用与xlsx兼容的openpyxl模块执行上述代码的另一种方法。 From what I've seen so far, it also keeps formatting. 从我到目前为止看到的,它也保持格式化。

from openpyxl import load_workbook
wb = load_workbook('names.xlsx')
ws = wb['SheetName']
ws['A1'] = 'A1'
wb.save('names.xlsx')

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

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