C:\Python\Python36-32\Scripts>pip install XlsxWriter
開始安裝
Collecting XlsxWriter
Downloading XlsxWriter-1.0.0-py2.py3-none-any.whl (139kB)
44% |██████████████ | 61kB 367kB/s eta 0:00:0
51% |████████████████▌ | 71kB 379kB/s eta 0:0
58% |██████████████████▉ | 81kB 413kB/s eta 0
66% |█████████████████████▏ | 92kB 458kB/s et
73% |███████████████████████▌ | 102kB 478kB/s
80% |█████████████████████████▉ | 112kB 669kB
88% |████████████████████████████▏ | 122kB 79
95% |██████████████████████████████▌ | 133kB
100% |████████████████████████████████| 143k
B 581kB/s
Installing collected packages: XlsxWriter
Successfully installed XlsxWriter-1.0.0
C:\Python\Python36-32\Scripts>
import xlsxwriter # create a excel file workbook = xlsxwriter.Workbook('test.xlsx') # create 5 worksheet for sheet_index in range(1, 5+1): worksheet = workbook.add_worksheet('Sheet ' + str(sheet_index)) # write data to each worksheet for sheet_index in range(1, 5+1): # switch to worksheet worksheet = workbook.get_worksheet_by_name('Sheet ' + str(sheet_index)) for row_index in range(1, 3+1): worksheet.write('A' + str(row_index), 'Column ' + str(sheet_index) + ' row ' + str(row_index)) # close excel workbook.close()
如果要讀文字檔的話用下列語法即可
with open('test.txt') as file: for line in file: print(line.rstrip()) # or do what you want
沒有留言:
張貼留言