不能使用openpyxl保存excel文件

我在使用openpyxl保存Excel文件时遇到问题。 我试图创build一个处理脚本,将从一个Excel文件中抓取数据,将其转储到一个转储Excel文件,并在Excel中的公式调整后,我将在转储Excel文件中的所有处理的数据。 我目前的代码是这样的。

from openpyxl import load_workbook import os import datetime from openpyxl.cell import get_column_letter, Cell, column_index_from_string, coordinate_from_string dump = dumplocation desktop = desktoplocation date = datetime.datetime.now().strftime("%Y-%m-%d") excel = load_workbook(dump+date+ ".xlsx", use_iterators = True) sheet = excel.get_sheet_by_name("Sheet1") try: query = raw_input('How many rows of data is there?\n') except ValueError: print 'Not a number' #sheetname = raw_input('What is the name of the worksheet in the data?\n') for filename in os.listdir(desktop): if filename.endswith(".xlsx"): print filename data = load_workbook(filename, use_iterators = True) ws = data.get_sheet_by_name(name = '17270115') #copying data from excel to data excel n=16 for row in sheet.iter_rows(): for cell in row: for rows in ws.iter_rows(): for cells in row: n+=1 if (n>=17) and (n<=32): cell.internal_value = cells.internal_value #adding column between time in UTC and the data column_index = 1 new_cells = {} sheet.column_dimensions = {} for coordinate, cell in sheet._cells.iteritems(): column_letter, row = coordinate_from_string(coordinate) column = column_index_from_string(column_letter) # shifting columns if column >= column_index: column += 1 column_letter = get_column_letter(column) coordinate = '%s%s' % (column_letter, row) # it's important to create new Cell object new_cells[coordinate] = Cell(sheet, column_letter, row, cell.value) sheet.cells = new_cells #setting columns to be hidden for coordinate, cell in sheet._cells.iteritems(): column_letter, row = coordinate_from_string(coordinate) column = column_index_from_string(column_letter) if (column<=3) and (column>=18): column.set_column(column, options={'hidden': True}) 

自从我两三个星期前刚开始Python以来,我的许多代码都是凌乱的。 我也有一些我可以在以后处理的突出问题。 这似乎并不是很多人为我的目的使用openpyxl。 我尝试使用正常的工作簿模块,但似乎没有工作,因为你不能迭代单元格项目。 (这是我需要从一个Excel文件复制和粘贴相关数据到另一个)

更新:我意识到,openpyxl只能创build工作簿,但不能编辑当前的。 所以我已经决定改变曲调,编辑新的工作簿后,我已经转移到那里的数据。 我导致使用回到工作簿来传输数据:

 from openpyxl import Workbook from openpyxl import worksheet from openpyxl import load_workbook import os from openpyxl.cell import get_column_letter, Cell, column_index_from_string, coordinate_from_string dump = "c:/users/y.lai/desktop/data/201501.xlsx" desktop = "c:/users/y.lai/desktop/" excel = Workbook() sheet = excel.add_sheet try: query = raw_input('How many rows of data is there?\n') except ValueError: print 'Not a number' #sheetname = raw_input('What is the name of the worksheet in the data?\n') for filename in os.listdir(desktop): if filename.endswith(".xlsx"): print filename data = load_workbook(filename, use_iterators = True) ws = data.get_sheet_by_name(name = '17270115') #copying data from excel to data excel n=16 q=0 for x in range(6,int(query)): for s in range(65,90): for cell in Cell(sheet,chr(s),x): for rows in ws.iter_rows(): for cells in rows: q+=1 if q>=5: n+=1 if (n>=17) and (n<=32): cell.value = cells.internal_value 

但是,这似乎还没有工作

  Traceback (most recent call last): File "xxx\Desktop\xlspostprocessing.py", line 40, in <module> for cell in Cell(sheet,chr(s),x): File "xxx\AppData\Local\Continuum\Anaconda\lib\site-packages\openpyxl\cell.py", line 181, in __init__ self._shared_date = SharedDate(base_date=worksheet.parent.excel_base_date) AttributeError: 'function' object has no attribute 'parent' 

通过API,但是..我被编码在那里淹没,所以我不能很好的API的意义。 对我来说,看起来我错误地使用了Cell模块。 我读了单元格及其属性的定义,从而使用字母表给出26个字母AZ。

您可以使用标准工作簿模式进行迭代。 use_iterators=True已被重命名为read_only=True以强调此模式用于(根据需要读取部件)。

由于工作簿是只读的, cell.internal_value始终是只读属性,因此您的代码无法用于此方法。

但是,看起来你没有那么远,因为你的Excel文件有问题。 您可能想要提交其中一个文件的错误。 邮件列表也许是一个更好的讨论的地方。

你可以尝试使用xlrd和xlwt来代替pyopenxl,但是你可能会发现你正在使用xlutil的东西 – 所有的东西都来自python-excel 。