Python脚本通过自动填充列中的空格来操作Excel表格

您好我有一个类似的结构,如下所示的Excel文件:

Location column2 column3 1 South Africa 2 3 4 5 England 6 7 8 9 US 10 11 12 

我想写一个python脚本,可以填写每个位置之间的空间与前面的位置的名称(即填写空间从2到4与南非的位置,6-8将填写与英格兰作为位置等)

如果有人能把我指向正确的方向,我将不胜感激。谢谢

好吧,我认为答案是我为xlrd制作的这个愚蠢的包装(或者,你自己写的!)。 关键是函数每次读取一行到一个列表中,并且Python列表记住它们被填充的顺序。 包装器生成一个字典,它将Excel工作表名称映射到该工作表上的行列表(我们假设每张工作表有一个表格,否则就不得不推广)。 每一行都是一个字典,其键是列名。

对于你,我读了你的数据,然后做这样的事情(未testing):

 import see_below as sb dict = sb.workbookToDict(your_file) output = [] this_location = None for row in dict[relevant_sheet_name]: output_row = row if row['Location'] is not None: this_location = row['Location'] else: output_row['Location'] = this_location 

可能会有一些可爱的东西,你可以用列表理解,但我有太多的葡萄酒,愚弄今晚:)

这是读者的包装:

 import xlrd def _isEmpty(_): return '' def _isString(element): return element.value.encode('ascii', 'ignore') def _isFloat(element): return float(element.value) def _isDate(element): import datetime rawDate = float(element.value) return (datetime.datetime(1899, 12, 30) + datetime.timedelta(days=rawDate)) def _isBool(element): return element.value == 1 def _isExcelGarbage(element): return int(element.value) _options = {0: _isEmpty, 1: _isString, 2: _isFloat, 3: _isDate, 4: _isBool, 5: _isExcelGarbage, 6: _isEmpty} def WorkbookToDict(filename): ''' Reads .xlsx file into dictionary. The keys of the dictionary correspond to sheet names in the Excel workbook. The first row of the Excel workbook is taken to be column names, and each row of the worksheet is read into a separate dictionary, whose keys correspond to column names. The collection of dictionaries (as a list) forms the value in the dictionary. The output maps sheet names (keys) to a collection of dictionaries (value). ''' book = xlrd.open_workbook(filename) allSheets = {} for s in book.sheets(): thisSheet = [] headings = [_options[x.ctype](x) for x in s.row(0)] for i in range(s.nrows): if i == 0: continue thisRow = s.row(i) if len(thisRow) != len(headings): raise Exception("Mismatch between headings and row length in ExcelReader") rowDict = {} for h, r in zip(headings, thisRow): rowDict[h] = _options[r.ctype](r) thisSheet.append(rowDict) allSheets[str(s.name)] = thisSheet return allSheets 

作者在这里:

 import xlwt def write(workbookDict, colMap, filename): ''' workbookDict should be a map of sheet names to a list of dictionaries. Each member of the list should be a mapping of column names to contents, missing keys are handled with the nullEntry field. colMap should be a dictionary whose keys are identical tto the sheet names in the workbookDict. Each value is a list of column names that are assumed to be in order. If a key exists in the workbookDict that does not exist in the colDict, the entry in workbookDict will not be written. ''' workbook = xlwt.Workbook() for sheet in workbookDict.keys(): worksheet = workbook.add_sheet(sheet) cols = colMap[sheet] i = 0 writeCols = True while i <= len(workbookDict[sheet]): if writeCols: for j in range(len(cols)): if writeCols: # write col headings worksheet.write(i, j, cols[j]) writeCols = False else: for j in range(len(cols)): worksheet.write(i, j, workbookDict[sheet][(i-1)][cols[j]]) i += 1 workbook.save(filename) 

无论如何,我真的希望这对你有用!

 wb = openpyxl.load_workbook('enter your workbook name') sheet = wb.get_sheet_by_name('enter your sheet name') row=sheet.max_row for row in range (3,row): if sheet.cell(row=row, column=1).value is not None and sheet.cell(row=row+1,column=1).value is None: sheet.cell(row=row+1, column=1).value = sheet.cell(row=row, column=1).value wb.save('enter your workbook name')