用Python插入文本到Excel中创buildBubbles

使用Python将文本input到Excel文件中,Excel 2010中出现了一些小问题。但是,它与Excel 2013一起工作。我已经在两台不同的PC上进行了testing。 在一台装有Excel 2010的电脑上,Excel崩溃,另一台电脑会进入这些气泡(它们的行为就像图像 – 你甚至可以调整它们的大小):

在这里输入图像说明

代码的工作原理是这样的:我创build一个string,用\r\n表示行改变,并粘贴Excel.DispatchPasteSpecial

什么可能是不正确插入的原因?

 def pasteToExcel(excelfile, sheet, data, startcell): if type(data) == list: rows = 1 cols = len(data) elif type(data) == pd.DataFrame: rows = data.shape[0] cols = data.shape[1] elif type(data) == int or float: rows = 1 cols = 1 elif type(data) == str: rows = 1 cols = 1 cellrange = startcell text = "" cellrange = calculateCellRange(startcell,rowcount=rows,colcount=cols) if type(data) == pd.DataFrame: print " Erkannter Datentyp vom Input: pd.DataFrame" line_strings = [] for row in range(rows): for col in range(cols-1): line_strings.append(str(data.ix[row,col])+"\t") #cell change: \t line_strings.append(str(data.ix[row,data.shape[1]-1])+"\r\n") for item in line_strings: item = item.replace(".",",") text += item elif type(data) == str: text = data elif type(data) == list: line_strings = [] for index in range(len(data)): # 0 1 2 len(data) =3 ; range(2) = 0,1 if index < len(data)-1: line_strings.append(str(data[index])+"\t") #cell change: \t elif index == len(data)-1: line_strings.append(str(data[index])+"\r\n") for item in line_strings: item = item.replace(".",",") text += item clipboard.OpenClipboard() clipboard.EmptyClipboard() clipboard.SetClipboardText(text) clipboard.CloseClipboard() excel = Dispatch("Excel.Application") excel.Visible = 0 wb = excel.Workbooks.Open(excelfile) ws = wb.Worksheets(sheet) ws.Activate() ws.Range(cellrange).Select() wb.ActiveSheet.PasteSpecial() excel.DisplayAlerts = False excel.ActiveWorkbook.Save() excel.Quit()