是否有任何方法使用python在Excel工作簿中拆分/拆分單元格?我想解釋如下 -如何使用python在excel工作簿中拆分合並的單元格
結果應符合以下條目的新的Excel文件 -
使用xlrd所有合併列是複製相同的字符串我的解決辦法如下所示 -
[注意:「formatted_info = True」標誌尚未在xlrd中實現,因此我無法直接獲取合併單元的列表..我不是s 。upposed升級xlrd上設置]
def xlsx_to_dict():
workbook = xlrd.open_workbook(xlsfile)
worksheet_names = workbook.sheet_names()
for worksheet_name in worksheet_names:
worksheet = workbook.sheet_by_name(worksheet_name)
num_rows = worksheet.nrows - 1
num_cells = worksheet.ncols - 1
curr_row = -1
header_row = worksheet.row(0)
columns = []
for cell in range(len(header_row)):
value = worksheet.cell_value(0, cell)
columns.append(value)
cities = []
for row in range(1,num_rows):
value = worksheet.cell_value(row,0)
type = worksheet.cell_type(row,0)
if not value == "":
cities.append(value)
names = []
for row in range(1,num_rows):
value = worksheet.cell_value(row,1)
type = worksheet.cell_type(row,1)
if not value == "":
names.append(value)
current_city = cities[0]
result_dict = {}
for curr_row in range(1,num_rows):
row = worksheet.row(curr_row)
curr_cell = -1
curr_name = names[0]
while curr_cell < num_cells:
curr_cell += 1
cell_value = worksheet.cell_value(curr_row, curr_cell)
if cell_value in cities and curr_cell == 0:
current_city = cell_value
if not result_dict.has_key(current_city):
result_dict[current_city] = {}
continue
if cell_value == "" and curr_cell == 0:
continue
if cell_value in names and curr_cell == 1:
curr_name = cell_value
if not result_dict[current_city].has_key(curr_name):
result_dict[current_city][curr_name] = {}
continue
if cell_value == "" and curr_cell == 1:
continue
try:
result_dict[current_city][curr_name]['Phone'].append(cell_Value)
except:
result_dict[current_city][curr_name]['Phone'] = [cell_value]
上述函數將返回如下的Python字典 -
{ 'New York' : { 'Tom' : [92929292, 33929] }, ........}
我會再遍歷目錄,並寫入新的Excel。
但是,我想要一些拆分合並單元格的通用方法。
請分享您迄今嘗試過的方法嗎?否則,人們會繼續向下投票 – Gogo