简体   繁体   中英

How to write a column header in Excel from an existing file in Python

I have this script that scrapes some text and print a list of names in an excel file. I couldn't figure out how to print a column name that doesn't come from scraped text. The following writes the scraped list correctly:

    block = [h5.get_text() for h5 in main_block.find_all('h5', class_='Company_line-title')]
    worksheet.write_column(1,0,block)
    workbook.close()

I wanted to add in the first column and row the name "Company". I tried to create a list like: comp = ['Company'] worksheet.write_column(0,0,comp)

but it didn't work.

for one cell use write() with cell number and use string variable instead of a list. exm below:

worksheet.write('A1', 'Company')  #OR worksheet.write(0, 0, 'Company')  

or

var='Company'
worksheet.write('A1', var)

my code:

li=['A','B','C']

workbook   = xlsxwriter.Workbook('filename.xlsx')

worksheet = workbook.add_worksheet()
worksheet.write('A1', 'Company')  #OR worksheet.write(0, 0, 'Company')  
worksheet.write_column(1,0,li)

excel file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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