Tag: xlutils

使用Python的xlrd和xlutils保存Excel格式的问题

简而言之,我想将一个Excel文件的所有格式保存到另一个文件中。 但是,尽pipe使用了formatting_info=True标志,但格式只对所更改的行内的所有未更改的单元格显示。 有什么build议? import xlrd, xlutils from xlrd import open_workbook from xlutils.copy import copy inBook = xlrd.open_workbook(r"path/to/file/format_input.xls", formatting_info=True, on_demand=True) outBook = xlutils.copy.copy(inBook) outBook.get_sheet(0).write(0,0,'changed!') outBook.save(r"path/to/file/format_output.xls") 在这里input图像说明

python xlrd / xlwt使用来自2个保存格式的不同工作簿的工作表创build新的工作簿

首先让我解释我的术语。 Excel工作簿包含工作表。 例如,新的Excel工作簿默认包含3张纸。 现在,使用xlrd,xlwt和xlutils,我的目的是输出一个新的工作簿(比如:file3),从file1input3张,从file2输出1张。 这尽可能保留格式。 我正在使用下面的代码(file1,file2,你必须自己手动创build,只需填充数字和文本): import os import xlrd import xlwt from xlutils.copy import copy as xlutils_copy from copy import deepcopy as deep_copy new_workbook = xlwt.Workbook() with xlrd.open_workbook("file1.xls", formatting_info=True) as rb1: wb1 = xlutils_copy(rb1) allSheets = [] allSheets.append(wb1.get_sheet(0)) allSheets.append(wb1.get_sheet(1)) allSheets.append(wb1.get_sheet(2)) extra = deep_copy(wb1.get_sheet(1)) allSheets.append(extra) allSheets[-1].name = 'extra sheet file1' with xlrd.open_workbook("file2.xls", formatting_info=True) as rb2: […]

xlutils.copy

我是新来的python(和一般编程)。 我有一个问题,使用xlrd , xlwt和xlutils来访问xlsx工作簿(这是一个常见的问题,但我没有find任何解决scheme)。 我应该改变我的包为py-excel吗? 在那种情况下,哪一个? 这是我的代码: import xlrd import xlwt from xlutils.copy import copy as xlutils_copy rd = xlrd.open_workbook("x:/PROJECTS/Papers/2014_Pasture/a.xlsx") rdsh = rd.sheet_by_name("FR_PASTURE") wrb = xlutils_copy(rd) ws = wrb.get_sheet_by_name("FR_PASTURE") 而我收到的错误: Traceback (most recent call last): File "X:\PROJECTS\Papers\2014_Pasture\AdjustXLSStats.py", line 28, in <module> wrb = xlutils_copy(rd) File "C:\Python27\lib\site-packages\xlutils-1.7.0-py2.7.egg\xlutils\copy.py", line 19, in copy w File "C:\Python27\lib\site-packages\xlutils-1.7.0-py2.7.egg\xlutils\filter.py", line 937, […]

使用python写入特定的Excel工作表

我想覆盖已经存在的excel文件中的特定单元格。 我search并find了这个答案, 使用xlwt写入现有的工作簿 。 我已经应用它如下, def wrtite_to_excel (self): #first I must open the specified excel file, sice open_file is in the same class, hence we can get it using self.sheet. bookwt = copy(self.workbook) sheetwt= bookwt.get_sheet(0) #now, I must know the column that was estimated so I overwrite it, colindex= self.columnBox.current() #returns the index of the […]

使用python在单个Excel单元格中格式化单个字符

我在Python 2.7上使用Windows Vista操作系统上的xlrd,xlwt和xlutils。 我在Excel工作表中有一组长度为100个字符的DNA序列,每个序列在一个单元格中。 我试图突出显示在Excel中的每个这些序列中的特定位置的字符(加粗或改变颜色),但还没有find一种方法来格式化单元格内的单个字符。 据我所知,应用样式将其应用于整个单元格。 因此,我试图将序列分解成单独的组件,其中序列的某些部分将被样式修改,而其他部分则不会,然后将这些序列整理成单个单元格中的单个string。 码: rb = open_workbook('Mybook',formatting_info = True) rs = rb.sheet_by_index(0) wb = copy(rb) ws = wb.get_sheet(0) minus35style = xlwt.easyxf('font:bold 1')#style我只想要一个字符 对于范围(0,368,1)中的b: rscellin = rs.cell(b,9) f = rscellin.value tominus35 = str(f [0:34]) minus35 = str(f [35:36]) ws.write(b,14,tominus35) ws.write(b,14,minus35,minus35style) wb.save( 'Mybook') 我的问题是添加样式会改变整个单元格,而我只想修改某些字符。 此外,后续写入同一个单元格会覆盖之前的内容。 有没有一种方法可以修改单个单元格中的单个字符,或将不同格式的文本添加到已有文本的现有单元格中? 请让我知道,如果你需要额外的信息,我忽略了。 我提前感谢你的时间。 布雷特

Python的Excel(xlrd,xlwt) – 如何从一个单元格复制一个样式,并把它放在另一个

具体来说,我试图打开一个现有的工作簿,并写入一些数据。 但是,无论何时写入数据,都会抹掉这些单元格上的边界。 所以我想知道是否有方法复制该单元格的样式,然后再写入它,然后重新应用它。 我想我可能会在这段代码的正确轨道上? from xlrd import open_workbook from xlwt import easyxf from xlutils.copy import copy from xlutils.styles import Styles rb=open_workbook('source.xls',formatting_info=True) styles = Styles(rb) rs=rb.sheet_by_index(0) wb=copy(rb) ws=wb.get_sheet(0) for i,cell in enumerate(rs.col(2)): if not i: continue cell_style = styles[rs.cell(i,2)] ws.write(i,2,cell.value,cell_style) wb.save('output.xls') 但是我得到这个错误: AttributeError: NamedStyle instance has no attribute 'font'