openpyxl的版本是:openpyxl-2.5.0a1
我的目的是将txt中的文本寫入excel中,其實是一個自己想實作的一個小功能
但是從txt檔案寫入excel2007,背景列印順序正常,但是打開excel實際資料沒有按照順序排列。
一直想不通,是以向大家提問。
圖1 是txt打開的, 圖2是生成後excel檔案,圖三是背景運作列印的資料。
圖三在【E1】列印了Remark,這個不知道如何出現的,餘下的,是按照正常的順序列印。但是到了excel中出現了順序不一緻的情況。望大牛解答。感謝!
import openpyxl
loadActive = open('F:\\pythoncode\\ActivityList.txt', encoding="utf-16")
wb = openpyxl.load_workbook('F:\\pythoncode\\example_1.xlsx')
# ' '.join(ActiveList)
dataActive = loadActive.readlines()
sheet = wb.get_active_sheet()
c = []
for i in range(len(dataActive)):
c += dataActive[i].split('\t')
j = 1
columnNum = 1
for i in range(int(len(c))):
if i < 3:
sheet.cell(row = j,column = columnNum).value = c[i]
columnNum += 1
else:
if i % 4 == 0:
j += 1
columnNum = 1
sheet.cell(row = j,column = columnNum).value = c[i]
print(sheet.cell(row = j,column = columnNum))
print(c[i])
else:
sheet.cell(row = j,column = columnNum).value = c[i]
columnNum += 1
print(sheet.cell(row = j,column = columnNum))
print(c[i])
loadActive.close()
wb.save('F:\\pythoncode\\example_1.xlsx')
Id ActivityId TimeDesc Remark
int_cs int_c string_c string
note Actid timessss memo
1 2 9:00-9:40 hello1
2 13 10:00-10:40 hello2
3 43 11:30-11:45 hello3
4 22 12:00-14:00 hello4
5 43 12:30-12:45 hello5
6 2 15:00-15:40 hello6
7 13 16:00-16:40 hello7
8 43 17:30-17:45 hello8
9 22 18:00-20:00 hello9
10 43 18:30-18:45 hello10
11 25 20:15 hello11
12 23 20:30-20:45 hello12
13 1 20:45-21:15 hello13