简体   繁体   English

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

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

I would like to print a series in my excel which goes like我想在我的 excel 中打印一个系列,就像

OUTPUT OUTPUT

Data Set 1 copy.jpg

Data Set 2 copy.jpg

This is the code that I have written:这是我写的代码:

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()

but it does not create any excel file after i run this code.但在我运行此代码后,它不会创建任何 excel 文件。 Thank you谢谢

That's because there is an error in your code.那是因为您的代码中有错误。 Below are the error and corrected code -以下是错误和更正的代码 -

You cannot concatenate 'str' and 'int' objects您不能连接 'str' 和 'int' 对象

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

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

You can also change a to str() for that concatenation:您还可以将该连接a更改为str()

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM