我在openpyxl中保存Excel文件时遇到问题。 我试图创建一个处理脚本,它将从一个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文件复制并粘贴到另一个excel文件中)
更新我意识到openpyxl只能创建工作簿,但无法编辑当前工作簿。所以我决定在将数据传输到那里之后更改音乐并编辑新工作簿。我已经使用返回工作簿来传输数据:
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模块。我读了Cell的定义及其属性,因此让chr(s)给出26个字母A-Z。
答案 0 :(得分:1)
您可以使用标准工作簿模式进行迭代。 use_iterators=True
已重命名为read_only=True
,以强调此模式的用途(按需读取部分)。
您的代码无法使用此方法,因为工作簿是只读的,cell.internal_value
始终是只读属性。
但是,您似乎没有那么远,因为您的Excel文件存在问题。您可能想要提交其中一个文件的错误。邮件列表也可能是一个更好的讨论场所。
答案 1 :(得分:0)
您可以尝试使用xlrd和xlwt而不是pyopenxl,但您可能会发现xlutil中已经提供的确切内容 - 所有内容均来自python-excel。