简体   繁体   English

如何使用 xlsxwriter(没有熊猫)将一组列表写入 Excel?

[英]How to write a set of lists to Excel using xlsxwriter (without pandas)?

I created 3 separated lists and appended them into a empty.我创建了 3 个单独的列表并将它们附加到一个空列表中。

I would like to write each list in Excel under the column Company , Sector , URL .我想在 Excel 中的CompanySectorURL列下写下每个列表。

I tried to iterate them without success.我试图迭代它们但没有成功。

I'd like to do it without going through Pandas.我想在不通过 Pandas 的情况下做到这一点。


    import xlsxwriter

    workbook = xlsxwriter.Workbook('example.xlsx')
    worksheet = workbook.add_worksheet('To add')

    companies = ['House1', 'House2']
    sector = ['Kitchen', 'Living room']
    url = ['www.house1.com', 'www.house2.com']

    list_companies = []
    list_companies.append(companies)
    list_companies.append(sector)
    list_companies.append(url)

    head = ['Company', 'Sector', 'URL']
    bold = workbook.add_format({'bold':True})

    worksheet.write_row(0,0,head, bold)
    workbook.close()

Converted lists aggregation to a dictionary for convenience.为方便起见,将列表聚合转换为字典。

To write to excel use the worksheet.write(row, col, *args) as follows:要写入 excel 使用worksheet.write(row, col, *args)如下:

import xlsxwriter

workbook = xlsxwriter.Workbook('example.xlsx')
worksheet = workbook.add_worksheet('To add')

companies = ['House1', 'House2']
sectors = ['Kitchen', 'Living room']
urls = ['www.house1.com', 'www.house2.com']

companies_dictionary = {}

companies_dictionary['companies']   = companies
companies_dictionary['sector']      = sectors
companies_dictionary['url']         = urls

head = ['Company', 'Sector', 'URL']
bold = workbook.add_format({'bold': True})

worksheet.write_row(0, 0, head, bold)


head_offset = 1
for company_index in range(len(companies_dictionary['companies'])):
    current_excel_row_number = company_index + head_offset + 1
    worksheet.write(f'A{current_excel_row_number}', companies_dictionary['companies'][company_index])
    worksheet.write(f'B{current_excel_row_number}', companies_dictionary['sector'][company_index])
    worksheet.write(f'C{current_excel_row_number}', companies_dictionary['url'][company_index])

workbook.close()

Result:结果:

Company Sector  URL
House1  Kitchen www.house1.com
House2  Living room www.house2.com

The cleanest way is probably to use the write_column() method.最干净的方法可能是使用write_column()方法。 Like this:像这样:


import xlsxwriter

workbook = xlsxwriter.Workbook('example.xlsx')
worksheet = workbook.add_worksheet('To add')

companies = ['House1', 'House2']
sector = ['Kitchen', 'Living room']
url = ['http://www.house1.com', 'http://www.house2.com']

head = ['Company', 'Sector', 'URL']
bold = workbook.add_format({'bold':True})

# Make some of the columns wider for clarity.
worksheet.set_column(0, 1, 12)
worksheet.set_column(2, 2, 20)

# Write the data.
worksheet.write_row(0,0,head, bold)
worksheet.write_column(1, 0, companies)
worksheet.write_column(1, 1, sector)
worksheet.write_column(1, 2, url)

workbook.close()

Output:输出:

在此处输入图片说明

Note, I added http:// to the urls so that they are converted by XlsxWriter to Excel links.请注意,我在 url 中添加了http:// ,以便 XlsxWriter 将它们转换为 Excel 链接。

For writing data with XlsxWriter see the Working with and Writing Data section of the XlsxWriter docs.有关使用 XlsxWriter 写入数据的信息,请参阅 XlsxWriter 文档的使用和写入数据部分。

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

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