为什么80%的码农都做不了架构师?>>>

关于写excel的格式控制,比如颜色等等

import xlwt

from datetime import datetime

font0 = xlwt.Font()

font0.name = 'Times New Roman'

font0.colour_index = 2

font0.bold = True

style0 = xlwt.XFStyle()

style0.font = font0

style1 = xlwt.XFStyle()

style1.num_format_str = 'D-MMM-YY'

wb = xlwt.Workbook()

ws = wb.add_sheet('A Test Sheet')

ws.write(0, 0, 'Test', style0)

ws.write(1, 0, datetime.now(), style1)

ws.write(2, 0, 1)

ws.write(2, 1, 1)

ws.write(2, 2, xlwt.Formula("A3+B3"))

wb.save('example.xls')

Examples Generating Excel Documents Using Python’s xlwt

Here are some simple examples using Python’s xlwt library to dynamically generate Excel documents.

Please note a useful alternative may be ezodf, which allows you to generate ODS (Open Document Spreadsheet) files for LibreOffice / OpenOffice. You can check them out at:http://packages.python.org/ezodf/index.html

The Simplest Example

import xlwt

workbook = xlwt.Workbook(encoding = 'ascii')

worksheet = workbook.add_sheet('My Worksheet')

worksheet.write(0, 0, label = 'Row 0, Column 0 Value')

workbook.save('Excel_Workbook.xls')

Formatting the Contents of a Cell

import xlwt

workbook = xlwt.Workbook(encoding = 'ascii')

worksheet = workbook.add_sheet('My Worksheet')

font = xlwt.Font() # Create the Font

font.name = 'Times New Roman'

font.bold = True

font.underline = True

font.italic = True

style = xlwt.XFStyle() # Create the Style

style.font = font # Apply the Font to the Style

worksheet.write(0, 0, label = 'Unformatted value')

worksheet.write(1, 0, label = 'Formatted value', style) # Apply the Style to the Cell

workbook.save('Excel_Workbook.xls')

Attributes of the Font Object

font.bold = True # May be: True, False

font.italic = True # May be: True, False

font.struck_out = True # May be: True, False

font.underline = xlwt.Font.UNDERLINE_SINGLE # May be: UNDERLINE_NONE, UNDERLINE_SINGLE, UNDERLINE_SINGLE_ACC, UNDERLINE_DOUBLE, UNDERLINE_DOUBLE_ACC

font.escapement = xlwt.Font.ESCAPEMENT_SUPERSCRIPT # May be: ESCAPEMENT_NONE, ESCAPEMENT_SUPERSCRIPT, ESCAPEMENT_SUBSCRIPT

font.family = xlwt.Font.FAMILY_ROMAN # May be: FAMILY_NONE, FAMILY_ROMAN, FAMILY_SWISS, FAMILY_MODERN, FAMILY_SCRIPT, FAMILY_DECORATIVE

font.charset = xlwt.Font.CHARSET_ANSI_LATIN # May be: CHARSET_ANSI_LATIN, CHARSET_SYS_DEFAULT, CHARSET_SYMBOL, CHARSET_APPLE_ROMAN, CHARSET_ANSI_JAP_SHIFT_JIS, CHARSET_ANSI_KOR_HANGUL, CHARSET_ANSI_KOR_JOHAB, CHARSET_ANSI_CHINESE_GBK, CHARSET_ANSI_CHINESE_BIG5, CHARSET_ANSI_GREEK, CHARSET_ANSI_TURKISH, CHARSET_ANSI_VIETNAMESE, CHARSET_ANSI_HEBREW, CHARSET_ANSI_ARABIC, CHARSET_ANSI_BALTIC, CHARSET_ANSI_CYRILLIC, CHARSET_ANSI_THAI, CHARSET_ANSI_LATIN_II, CHARSET_OEM_LATIN_I

font.colour_index = ?

font.get_biff_record = ?

font.height = 0x00C8 # C8 in Hex (in decimal) = 10 points in height.

font.name = ?

font.outline = ?

font.shadow = ?

Setting the Width of a Cell

import xltw

workbook = xlwt.Workbook()

worksheet = workbook.add_sheet('My Sheet')

worksheet.write(0, 0, 'My Cell Contents')

worksheet.col(0).width = 3333 # 3333 = 1" (one inch).

workbook.save('Excel_Workbook.xls')

Entering a Date into a Cell

import xlwt

import datetime

workbook = xlwt.Workbook()

worksheet = workbook.add_sheet('My Sheet')

style = xlwt.XFStyle()

style.num_format_str = 'M/D/YY' # Other options: D-MMM-YY, D-MMM, MMM-YY, h:mm, h:mm:ss, h:mm, h:mm:ss, M/D/YY h:mm, mm:ss, [h]:mm:ss, mm:ss.0

worksheet.write(0, 0, datetime.datetime.now(), style)

workbook.save('Excel_Workbook.xls')

Adding a Formula to a Cell

import xlwt

workbook = xlwt.Workbook()

worksheet = workbook.add_sheet('My Sheet')

worksheet.write(0, 0, 5) # Outputs 5

worksheet.write(0, 1, 2) # Outputs 2

worksheet.write(1, 0, xlwt.Formula('A1*B1')) # Should output "10" (A1[5] * A2[2])

worksheet.write(1, 1, xlwt.Formula('SUM(A1,B1)')) # Should output "7" (A1[5] + A2[2])

workbook.save('Excel_Workbook.xls')

Adding a Hyperlink to a Cell

import xlwt

workbook = xlwt.Workbook()

worksheet = workbook.add_sheet('My Sheet')

worksheet.write(0, 0, xlwt.Formula('HYPERLINK("http://www.google.com";"Google")')) # Outputs the text "Google" linking to http://www.google.com

workbook.save('Excel_Workbook.xls')

Merging Columns and Rows

import xlwt

workbook = xlwt.Workbook()

worksheet = workbook.add_sheet('My Sheet')

worksheet.write_merge(0, 0, 0, 3, 'First Merge') # Merges row 0's columns 0 through 3.

font = xlwt.Font() # Create Font

font.bold = True # Set font to Bold

style = xlwt.XFStyle() # Create Style

style.font = font # Add Bold Font to Style

worksheet.write_merge(1, 2, 0, 3, 'Second Merge', style) # Merges row 1 through 2's columns 0 through 3.

workbook.save('Excel_Workbook.xls')

Setting the Alignment for the Contents of a Cell

import xlwt

workbook = xlwt.Workbook()

worksheet = workbook.add_sheet('My Sheet')

alignment = xlwt.Alignment() # Create Alignment

alignment.horz = xlwt.Alignment.HORZ_CENTER # May be: HORZ_GENERAL, HORZ_LEFT, HORZ_CENTER, HORZ_RIGHT, HORZ_FILLED, HORZ_JUSTIFIED, HORZ_CENTER_ACROSS_SEL, HORZ_DISTRIBUTED

alignment.vert = xlwt.Alignment.VERT_CENTER # May be: VERT_TOP, VERT_CENTER, VERT_BOTTOM, VERT_JUSTIFIED, VERT_DISTRIBUTED

style = xlwt.XFStyle() # Create Style

style.alignment = alignment # Add Alignment to Style

worksheet.write(0, 0, 'Cell Contents', style)

workbook.save('Excel_Workbook.xls')

Adding Borders to a Cell

# Please note: While I was able to find these constants within the source code, on my system (using LibreOffice,) I was only presented with a solid line, varying from thin to thick; no dotted or dashed lines.

import xlwt

workbook = xlwt.Workbook()

worksheet = workbook.add_sheet('My Sheet')

borders = xlwt.Borders() # Create Borders

borders.left = xlwt.Borders.DASHED # May be: NO_LINE, THIN, MEDIUM, DASHED, DOTTED, THICK, DOUBLE, HAIR, MEDIUM_DASHED, THIN_DASH_DOTTED, MEDIUM_DASH_DOTTED, THIN_DASH_DOT_DOTTED, MEDIUM_DASH_DOT_DOTTED, SLANTED_MEDIUM_DASH_DOTTED, or 0x00 through 0x0D.

borders.right = xlwt.Borders.DASHED

borders.top = xlwt.Borders.DASHED

borders.bottom = xlwt.Borders.DASHED

borders.left_colour = 0x40

borders.right_colour = 0x40

borders.top_colour = 0x40

borders.bottom_colour = 0x40

style = xlwt.XFStyle() # Create Style

style.borders = borders # Add Borders to Style

worksheet.write(0, 0, 'Cell Contents', style)

workbook.save('Excel_Workbook.xls')

Setting the Background Color of a Cell

import xlwt

workbook = xlwt.Workbook()

worksheet = workbook.add_sheet('My Sheet')

pattern = xlwt.Pattern() # Create the Pattern

pattern.pattern = xlwt.Pattern.SOLID_PATTERN # May be: NO_PATTERN, SOLID_PATTERN, or 0x00 through 0x12

pattern.pattern_fore_colour = 5 # May be: 8 through 63. 0 = Black, 1 = White, 2 = Red, 3 = Green, 4 = Blue, 5 = Yellow, 6 = Magenta, 7 = Cyan, 16 = Maroon, 17 = Dark Green, 18 = Dark Blue, 19 = Dark Yellow , almost brown), 20 = Dark Magenta, 21 = Teal, 22 = Light Gray, 23 = Dark Gray, the list goes on...

style = xlwt.XFStyle() # Create the Pattern

style.pattern = pattern # Add Pattern to Style

worksheet.write(0, 0, 'Cell Contents', style)

workbook.save('Excel_Workbook.xls')

TODO: Things Left to Document

- Panes -- separate views which are always in view

- Border Colors (documented above, but not taking effect as it should)

- Border Widths (document above, but not working as expected)

- Protection

- Row Styles

- Zoom / Manification

- WS Props?

Source Code for reference available at: https://secure.simplistix.co.uk/svn/xlwt/trunk/xlwt/

原文链接:http://blog.sina.com.cn/s/blog_5357c0af01019gjo.html

另:

关于设置写入单元格的背景色,摘录如下:

# Text values for colour indices. "grey" is a synonym of "gray".# The names are those given by Microsoft Excel 2003 to the colours# in the default palette. There is no great correspondence with# any W3C name-to-RGB mapping._colour_map_text = """/aqua 0x31black 0x08blue 0x0Cblue_gray 0x36bright_green 0x0Bbrown 0x3Ccoral 0x1Dcyan_ega 0x0Fdark_blue 0x12dark_blue_ega 0x12dark_green 0x3Adark_green_ega 0x11dark_purple 0x1Cdark_red 0x10dark_red_ega 0x10dark_teal 0x38dark_yellow 0x13gold 0x33gray_ega 0x17gray25 0x16gray40 0x37gray50 0x17gray80 0x3Fgreen 0x11ice_blue 0x1Findigo 0x3Eivory 0x1Alavender 0x2Elight_blue 0x30light_green 0x2Alight_orange 0x34light_turquoise 0x29light_yellow 0x2Blime 0x32magenta_ega 0x0Eocean_blue 0x1Eolive_ega 0x13olive_green 0x3Borange 0x35pale_blue 0x2Cperiwinkle 0x18pink 0x0Eplum 0x3Dpurple_ega 0x14red 0x0Arose 0x2Dsea_green 0x39silver_ega 0x16sky_blue 0x28tan 0x2Fteal 0x15teal_ega 0x15turquoise 0x0Fviolet 0x14white 0x09yellow 0x0D"""

python xlwt写excel_Python使用xlwt写excel并设置写入格式相关推荐

  1. 如何在 Excel 中设置表格格式并进行排序?

    欢迎观看 Microsoft Excel 教程,小编带大家学习 Microsoft Excel 的使用技巧,了解如何在 Excel 中设置表格格式并进行排序. 选择数据中的一个单元格,选择「开始」-「 ...

  2. excel怎么设置条件格式

    条件格式是指如果指定的单元格满足了特定的条件,Excel便将底纹.字体.颜色等格式用到该单元格中,一般需突出显示.计算结果或者要监视单元格的值时,可以使用条件格式.excel怎么设置条件格式的具体操作 ...

  3. java代码里的JSON格式怎么写好看_python3 循环读取excel文件并写入json操作

    文件内容: excel内容: 代码: import xlrdimport jsonimport operatordef read_xlsx(filename): # 打开excel文件 data1 = ...

  4. python:excel批量设置打印格式

    本篇博客介绍 xlwt 模块设置 Excel表格文件的默认打印格式.自动调整列宽. from openpyxl import load_workbook from openpyxl.utils imp ...

  5. 【EPPlus使用】之导出Excel,设置单元格式

    第一种 日期格式: cell.setCellValue(new Date());HSSFCellStyle cellStyle = demoWorkBook.createCellStyle();HSS ...

  6. python 漂亮的excel_python 自定义漂亮的 excel 结果测试报告

    起因 一直被测试报告的质量所困扰, python的htmltestrunner,效果不满意,不支持py3,要手动改些地方 自定义pyh去拼接html,代码非常多,看得眼花缭乱,不好调试 这几天一直在看 ...

  7. python openpyxl读取excel_python 使用openpyxl读取excel数据

    openpyxl介绍 ​ openpyxl是一个开源项目,它是一个用于读取/写入Excel 2010文档(如xlsx .xlsm .xltx .xltm文件 )的Python库,如果要处理更早格式的E ...

  8. python openpyxl读取excel_Python使用openpyxl读写excel文件

    这是一个第三方库,可以处理xlsx格式的Excel文件.pip install openpyxl安装.如果使用Aanconda,应该自带了. 1.读取Excel文件 默认打开的文件为可读写,若有需要可 ...

  9. python基础教程 excel_Python新手入门:Excel基本操作(二)

    写入 Excel 首先当然是安装第三方模块:pip install xlsxwriter 首先我们需要先创建一个 WorkBook:import xlsxwriter workbook = xlsxw ...

  10. 使用python 对图片进行水印,保护自己写的文章

    1,关于文章被爬 说起来挺桑心的,好不容易写的文章,被爬走. 用个搜索引擎搜索都不是在第一位,写的文章全给这些网站提供流量了. 这种网站还居多广告. 还是抱怨少点吧.csdn对于这些事情也是无所作为啊 ...

最新文章

  1. 图像对象paip.Image对象出现“对象当前正在其他地方使用或者GDI+中发生一般性错误的解决...
  2. 卡尔曼滤波MATLAB代码实现
  3. 《剑指offer》连续子数组的最大和
  4. 【重磅】ArcGIS 10.8手把手经典图文安装教程(附安装包全套装下载,亲测可用)
  5. P91--商品保存debug完成
  6. Python 程序 可以一直输入 quit_从零开始学Python - 第002课:第一个Python程序
  7. 使Docker容器拥有可被宿主机以外的机器直接访问的独立IP
  8. 为CheckBoxList每个项目添加一张图片
  9. idea修改完jdbc文件后没有更新_JDBC+MySQL入门案例
  10. Python+flask+flask-email发送带附件的电子邮件
  11. java为什么删除jpg删不掉_java-如何在不损失质量的情况下从图像(JPG)删除元数据?...
  12. java 是怎么在中删除下拉列表_java中下拉菜单如何清空
  13. java6_64.tar配置,Ubuntu 下Java-JDK6的安装与环境配置
  14. ELF 文件数据分析: 全局变量
  15. 怎么做一个专业的软件安装包?
  16. 51单片机排队叫号系统LCD1602显示仿真设计(proteus仿真+程序)
  17. 一树梨花压海棠的典故
  18. 数独高阶技巧之八——SDC
  19. apktool java_apktool 简单使用记录
  20. 关于DSP28335CCS6编译时出现error #10099-D: program will not fit into available memory.

热门文章

  1. 计算机公式与函数乘法,excel里减法函数是哪个?-excel函数公式乘法
  2. 常微分方程 伍卓群 题目
  3. 个人风景网站模板HTML+CSS+JS(源码)
  4. ubuntu20.04 NVIDIA显卡驱动安装教程(Y9000p)
  5. ubuntu中安装flash播放器
  6. c++ 11/14新特性
  7. 联想电脑(xx%电量可用已连接适配器,未充电)解决方法
  8. 彻底杀除“logo1_.exe”(威金病毒)病毒
  9. 如何在Word文档中制作三线表
  10. iframe 防止挂马的问题