python如何编写excel_如何用Python编写Excel

这篇博客介绍了如何使用Python的xlsxwriter和openpyxl库来创建和写入Excel文件。示例代码包括设置单元格格式、写入文本和数值,以及保存文件。
摘要由CSDN通过智能技术生成

是的,CSV和TSV文件非常容易处理,特别是与Excel相比,Excel中有各种各样的对象、格式等。请尝试使用下面的简单脚本来写入Excel文件。在import xlsxwriter

# Create an new Excel file and add a worksheet.

workbook = xlsxwriter.Workbook('C:/your_path/test.xlsx')

worksheet = workbook.add_worksheet()

# Widen the first column to make the text clearer.

worksheet.set_column('A:A', 20)

# Add a bold format to use to highlight cells.

bold = workbook.add_format({'bold': True})

# Write some simple text.

worksheet.write('A1', 'Hello')

# Text with formatting.

worksheet.write('A2', 'World', bold)

# Write some numbers, with row/column notation.

worksheet.write(2, 0, 123)

worksheet.write(3, 0, 123.456)

workbook.close()

***********************************************************************************

from openpyxl import Workbook

wb = Workbook()

# grab the active worksheet

ws = wb.active

# Data can be assigned directly to cells

ws['A1'] = 42

# Rows can also be appended

ws.append([1, 2, 3])

# Python types will automatically be converted

import datetime

ws['A2'] = datetime.datetime.now()

# Save the file

wb.save("C:\your_path\\sample.xlsx")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值