Home Backend Development Python Tutorial Python implements a method to customize the order and arrangement of writing data to Excel

Python implements a method to customize the order and arrangement of writing data to Excel

Apr 23, 2018 pm 03:13 PM
python arrangement customize

这篇文章主要介绍了Python实现自定义顺序、排列写入数据到Excel的方法,涉及Python针对Excel文件的数据处理及读写相关操作技巧,需要的朋友可以参考下

本文实例讲述了Python实现自定义顺序、排列写入数据到Excel的方法。分享给大家供大家参考,具体如下:

例1. 数据框顺序写入Excel:


data=a
import xlsxwriter
workbook = xlsxwriter.Workbook('F:/chart1.xlsx')
worksheet = workbook.add_worksheet('请求接口')
title = [u'订单号',u'债权编号',u'请求参数',u'创建时间',u'结果']
print data.iloc[:,0]
format=workbook.add_format()
format.set_border(1)
format_title=workbook.add_format()
format_title.set_border(1)
format_title.set_bg_color('#cccccc')
format_title.set_align('center')
format_title.set_bold()
format_ave=workbook.add_format()
format_ave.set_border(1)
format_ave.set_num_format('0.00')
worksheet.write_row('A1',title,format_title)
worksheet.write_column('A2:', data.iloc[:,0],format)
worksheet.write_column('B2', data.iloc[:,1],format)
worksheet.write_column('C2', data.iloc[:,2],format)
worksheet.write_column('D2', data.iloc[:,3],format)
worksheet.write_column('E2', data.iloc[:,4],format)
workbook.close()
Copy after login


例2. (自动报表):


#coding: utf-8
import xlsxwriter
workbook = xlsxwriter.Workbook('F:/chart.xlsx')
worksheet = workbook.add_worksheet()
chart = workbook.add_chart({'type': 'column'})
title = [u'业务名称',u'星期一',u'星期二',u'星期三',u'星期四',u'星期五',u'星期六',u'星期日',u'平均流量']
buname= [u'业务官网',u'新闻中心',u'购物频道',u'体育频道',u'亲子频道']
data = [
  [150,152,158,149,155,145,148],
  [89,88,95,93,98,100,99],
  [201,200,198,175,170,198,195],
  [75,77,78,78,74,70,79],
  [88,85,87,90,93,88,84],
]
print data
format=workbook.add_format()
format.set_border(1)
format_title=workbook.add_format()
format_title.set_border(1)
format_title.set_bg_color('#cccccc')
format_title.set_align('center')
format_title.set_bold()
format_ave=workbook.add_format()
format_ave.set_border(1)
format_ave.set_num_format('0.00')
worksheet.write_row('A1',title,format_title)
worksheet.write_column('A2', buname,format)
worksheet.write_row('B2', data[0],format)
worksheet.write_row('B3', data[1],format)
worksheet.write_row('B4', data[2],format)
worksheet.write_row('B5', data[3],format)
worksheet.write_row('B6', data[4],format)
def chart_series(cur_row):
  worksheet.write_formula('I'+cur_row, \
   '=AVERAGE(B'+cur_row+':H'+cur_row+')',format_ave)
  chart.add_series({
    'categories': '=Sheet1!$B$1:$H$1',
    'values':   '=Sheet1!$B$'+cur_row+':$H$'+cur_row,
    'line':    {'color': 'black'},
    'name':  '=Sheet1!$A$'+cur_row,
  })
for row in range(2, 7):
  chart_series(str(row))
chart.set_table()
chart.set_style(30)
chart.set_size({'width': 577, 'height': 287})
chart.set_title ({'name': u'业务流量周报图表'})
chart.set_y_axis({'name': 'Mb/s'})
worksheet.insert_chart('A8', chart)
workbook.close()
Copy after login

相关推荐:

Python实现针对给定字符串寻找最长非重复子串

python实现随机调用一个浏览器打开网页

The above is the detailed content of Python implements a method to customize the order and arrangement of writing data to Excel. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Does H5 page production require continuous maintenance? Does H5 page production require continuous maintenance? Apr 05, 2025 pm 11:27 PM

The H5 page needs to be maintained continuously, because of factors such as code vulnerabilities, browser compatibility, performance optimization, security updates and user experience improvements. Effective maintenance methods include establishing a complete testing system, using version control tools, regularly monitoring page performance, collecting user feedback and formulating maintenance plans.

What is the reason why PS keeps showing loading? What is the reason why PS keeps showing loading? Apr 06, 2025 pm 06:39 PM

PS "Loading" problems are caused by resource access or processing problems: hard disk reading speed is slow or bad: Use CrystalDiskInfo to check the hard disk health and replace the problematic hard disk. Insufficient memory: Upgrade memory to meet PS's needs for high-resolution images and complex layer processing. Graphics card drivers are outdated or corrupted: Update the drivers to optimize communication between the PS and the graphics card. File paths are too long or file names have special characters: use short paths and avoid special characters. PS's own problem: Reinstall or repair the PS installer.

How to solve the problem of loading when PS is started? How to solve the problem of loading when PS is started? Apr 06, 2025 pm 06:36 PM

A PS stuck on "Loading" when booting can be caused by various reasons: Disable corrupt or conflicting plugins. Delete or rename a corrupted configuration file. Close unnecessary programs or upgrade memory to avoid insufficient memory. Upgrade to a solid-state drive to speed up hard drive reading. Reinstalling PS to repair corrupt system files or installation package issues. View error information during the startup process of error log analysis.

How to speed up the loading speed of PS? How to speed up the loading speed of PS? Apr 06, 2025 pm 06:27 PM

Solving the problem of slow Photoshop startup requires a multi-pronged approach, including: upgrading hardware (memory, solid-state drive, CPU); uninstalling outdated or incompatible plug-ins; cleaning up system garbage and excessive background programs regularly; closing irrelevant programs with caution; avoiding opening a large number of files during startup.

Is slow PS loading related to computer configuration? Is slow PS loading related to computer configuration? Apr 06, 2025 pm 06:24 PM

The reason for slow PS loading is the combined impact of hardware (CPU, memory, hard disk, graphics card) and software (system, background program). Solutions include: upgrading hardware (especially replacing solid-state drives), optimizing software (cleaning up system garbage, updating drivers, checking PS settings), and processing PS files. Regular computer maintenance can also help improve PS running speed.

How to solve the problem of loading when PS is always showing that it is loading? How to solve the problem of loading when PS is always showing that it is loading? Apr 06, 2025 pm 06:30 PM

PS card is "Loading"? Solutions include: checking the computer configuration (memory, hard disk, processor), cleaning hard disk fragmentation, updating the graphics card driver, adjusting PS settings, reinstalling PS, and developing good programming habits.

What is the difference between H5 page production and WeChat applets What is the difference between H5 page production and WeChat applets Apr 05, 2025 pm 11:51 PM

H5 is more flexible and customizable, but requires skilled technology; mini programs are quick to get started and easy to maintain, but are limited by the WeChat framework.

Is Python or JavaScript better? Is Python or JavaScript better? Apr 06, 2025 am 12:14 AM

Python is more suitable for data science and machine learning, while JavaScript is more suitable for front-end and full-stack development. 1. Python is known for its concise syntax and rich library ecosystem, and is suitable for data analysis and web development. 2. JavaScript is the core of front-end development. Node.js supports server-side programming and is suitable for full-stack development.

See all articles