首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

Python与Excel(1)

欢迎关注YaK芽课

如何查看女神撤回的微信消息教学在文章后面,着急的小伙伴可以跳转到后面的部分学习。

Microsoft Excel已成为最流行的个人计算机数据处理软件,它能够方便的制作出各种电子表格,使用公式和函数对数据进行复杂的运算;用各种图表来表示数据直观明了;利用超级链接功能,用户可以快速打开局域网或Interner上的文件,与世界上任何位置的互联网用户共享工作薄文件。

数据处理是 Python 的一大应用场景,而 Excel 则是最流行的数据处理软件。因此用 Python 进行数据相关的工作时,难免要和 Excel 打交道。

Example 1: Hello World

最简单的电子表格。这是一个开始查看XLSX编写器模块是否正确安装的好地方。

import xlsxwriter

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

worksheet = workbook.add_worksheet()

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

workbook.close()

dffdf

Example 2: Simple Feature Demonstration

这个程序是编写XLSX编写器模块的一些特性的一个例子。

#

importxlsxwriter

# Create an new Excelfile and add a worksheet.

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

worksheet=workbook.add_worksheet()

# Widen the firstcolumn to make the text clearer.

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

# Add a bold format touse to highlight cells.

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

# Write some simpletext.

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)

# Insert an image.

worksheet.insert_image('B5','logo.png')

workbook.close()

笔记:

此示例包括通过格式化类使用单元格格式。

字符串和数字可以用相同的工作表写()方法编写。

可以使用行列表示法或“A1”样式表示法将数据写入单元格,请参见使用单元格表示法。

Example 3: Dates and Times in Excel

这个程序是编写XLSX编写器模块的一些特性的一个例子。有关日期和时间部分,请参阅关于此示例的更多细节。

fromdatetimeimportdatetime

importxlsxwriter

# Create a workbook andadd a worksheet.

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

worksheet=workbook.add_worksheet()

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

# Expand the firstcolumns so that the dates are visible.

worksheet.set_column('A:B',30)

# Write the columnheaders.

worksheet.write('A1','Formatted date',bold)

worksheet.write('B1','Format',bold)

# Create a datetimeobject to use in the examples.

date_time=datetime.strptime('2013-01-2312:30:05.123',

'%Y-%m-%d%H:%M:%S.%f')

# Examples date andtime formats. In the output file compare how changing

# the format codeschange the appearance of the date.

date_formats=(

'dd/mm/yy',

'mm/dd/yy',

'dd m yy',

'd mm yy',

'd mmmyy',

'd mmmmyy',

'd mmmmyyy',

'd mmmmyyyy',

'dd/mm/yyhh:mm',

'dd/mm/yyhh:mm:ss',

'dd/mm/yyhh:mm:ss.000',

'hh:mm',

'hh:mm:ss',

'hh:mm:ss.000',

)

# Start from first rowafter headers.

row=1

# Write the same dateand time using each of the above formats.

fordate_format_strindate_formats:

# Createa format for the date or time.

date_format=workbook.add_format({'num_format':date_format_str,

'align':'left'})

# Writethe same date using different formats.

worksheet.write_datetime(row,0,date_time,date_format)

# Alsowrite the format string for comparison.

worksheet.write_string(row,1,date_format_str)

row+=1

workbook.close()

芽课:用计算开启科学认知,展开生命智慧的大树。帮助孩子形成更好的科学素养和科研能力。让这些出生即数字公民的孩子,拥有释放自己无限想象力的能量。

欢迎关注YaK

用计算的力量改变世界是每一个程序员的梦想,YaK团队抱着对教育的敬仰和热忱,开发了有趣的YaK编程工具以及配套的系统化教学课程。让孩子可以用编程去学习和理解自然的语言:数学。

  • 发表于:
  • 原文链接https://kuaibao.qq.com/s/20180713G1ON9P00?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券