使用Python填写Excel文件的简便方法

Dna*_*iel 4 python linux macos excel

让我们说我有一个名为test.xlsx的excel文件,它是一个有三张工作簿,其中sheet1被称为hello1,sheet2被称为hello2,而sheet3被称为bye.

现在,我想读取该文件,然后重新编写相同的文件,但只更改名为hello2的工作表(第B列,第11行)和名为bye的工作表(第D列,第14行)中的值.我想要给出的值分别是'test'(一个字符串)和135(即,在工作表hello2中写入测试,在工作表中再写入14).

您可能想知道我为什么要问这样奇怪的问题,但基本上我希望获得以下一些技能/知识:

  • 能够使用python读取工作簿,然后读取excel文件的特定表
  • 能够使用python写入给定位置的Excel工作表

注意:作为参考,我可以在redhat服务器中使用任何版本的python,excel文件是用我的mac生成的,使用excel for mac 2011保存为xlsx格式.版本14.0.1,然后我将excel文件复制到redhat服务器.

sco*_*001 6

它看起来不像你正在做的是依赖于单元格中已有的值,因此xlrd除了打开书本外你根本不需要使用它.的方式xlrd,xlwtxlutils工作原理是使用xlrd读取的工作簿,然后使用xlutils做一个写副本.所以你的代码的快速模型看起来像:

import xlrd, xlwt
from xlutils.copy import copy

rb = xlrd.open_workbook("Path/To/Doc", formatting_info=True) #Make Readable Copy
wb = copy(rb) #Make Writeable Copy

ws1 = wb.get_sheet(1) #Get sheet 1 in writeable copy
ws1.write(1, 11, 'test') #Write 'test' to cell (1, 11)

ws2 = wb.get_sheet(2) #Get sheet 2 in writeable copy
ws2.write(3, 14, '135') #Write '135' to cell (3, 14)

wb.save("New/File/Path") #Save the newly written copy. Enter the same as the old path to write over
Run Code Online (Sandbox Code Playgroud)