简体   繁体   English

如何从 Python 中的现有文件在 Excel 中编写列标题

[英]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.我有这个脚本,可以抓取一些文本并在 excel 文件中打印名称列表。 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)我试图创建一个列表,如: 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.对于一个单元格,使用带有单元格编号的 write() 并使用字符串变量而不是列表。 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 excel文件

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

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