簡體   English   中英

想使用 python 在 excel 中打印一系列字符串

[英]want to print a series of string in excel using python

我想在我的 excel 中打印一個系列,就像

OUTPUT

Data Set 1 copy.jpg

Data Set 2 copy.jpg

這是我寫的代碼:

import itertools
import xlsxwriter

book = xlsxwriter.Workbook(r'E:\license8.xlsx')
sh = book.add_worksheet()
a = 1

for a in range (1,1320):
    ch = 'Data Set '+ a +' copy.jpg'
    sh.write(a,0,ch)
    a = a+1
book.close()

但在我運行此代碼后,它不會創建任何 excel 文件。 謝謝

那是因為您的代碼中有錯誤。 以下是錯誤和更正的代碼 -

您不能連接 'str' 和 'int' 對象

#erroneous line of code
ch = 'Data Set '+ a +' copy.jpg'

#use below code instead
ch = 'Data Set {} copy.jpg'.format(a)

您還可以將該連接a更改為str()

ch = 'Data Set ' + str(a) + ' copy.jpg'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM