xls2- 用Python读写Excel文件-乘法口诀
Posted xinxi2010
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了xls2- 用Python读写Excel文件-乘法口诀相关的知识,希望对你有一定的参考价值。
xls2- 用Python读写Excel文件
https://gitee.com/pandarrr/Panda.SimpleExcel
https://www.cnblogs.com/lhj588/archive/2012/01/06/2314181.html
一、安装xlrd模块
到python官网下载http://pypi.python.org/pypi/xlrd模块安装,前提是已经安装了python 环境。
二、使用介绍
1、导入模块
import xlrd
2、打开Excel文件读取数据
data = xlrd.open_workbook(\'excelFile.xls\')
3、使用技巧
获取一个工作表
三、Demo代码
Demo代码其实很简单,就是读取Excel数据。
1 # -*- coding: utf-8 -*-
2 import xdrlib ,sys
3 import xlrd
4 def open_excel(file= \'file.xls\'):
5 try:
6 data = xlrd.open_workbook(file)
7 return data
8 except Exception,e:
9 print str(e)
10 #根据索引获取Excel表格中的数据 参数:file:Excel文件路径 colnameindex:表头列名所在行的所以 ,by_index:表的索引
11 def excel_table_byindex(file= \'file.xls\',colnameindex=0,by_index=0):
12 data = open_excel(file)
13 table = data.sheets()[by_index]
14 nrows = table.nrows #行数
15 ncols = table.ncols #列数
16 colnames = table.row_values(colnameindex) #某一行数据
17 list =[]
18 for rownum in range(1,nrows):
19
20 row = table.row_values(rownum)
21 if row:
22 app = {}
23 for i in range(len(colnames)):
24 app[colnames[i]] = row[i]
25 list.append(app)
26 return list
27
28 #根据名称获取Excel表格中的数据 参数:file:Excel文件路径 colnameindex:表头列名所在行的所以 ,by_name:Sheet1名称
29 def excel_table_byname(file= \'file.xls\',colnameindex=0,by_name=u\'Sheet1\'):
30 data = open_excel(file)
31 table = data.sheet_by_name(by_name)
32 nrows = table.nrows #行数
33 colnames = table.row_values(colnameindex) #某一行数据
34 list =[]
35 for rownum in range(1,nrows):
36 row = table.row_values(rownum)
37 if row:
38 app = {}
39 for i in range(len(colnames)):
40 app[colnames[i]] = row[i]
41 list.append(app)
42 return list
43
44 def main():
45 tables = excel_table_byindex()
46 for row in tables:
47 print row
48
49 tables = excel_table_byname()
50 for row in tables:
51 print row
52
53 if __name__=="__main__":
54 main()
关于其他版本的excel,可以通过他提供的链接教程进行学习。
XlsxWriter:
https://github.com/jmcnamara/XlsxWriter
http://xlsxwriter.readthedocs.org
openpyxl: http://openpyxl.readthedocs.io/en/default/
Microsoft excel API:https://msdn.microsoft.com/en-us/library/fp179694.aspx
简介
xlrd用来读取excel文件,xlwt用来写excel文件,它们合作来对excel进行操作。
官方文档:http://www.python-excel.org/
xlrd官方介绍:https://pypi.python.org/pypi/xlrd/1.0.0
xlwt官方介绍:https://pypi.python.org/pypi/xlwt/1.1.2
xlutils官方介绍:https://pypi.python.org/pypi/xlutils
http://xlutils.readthedocs.io/en/latest/
1. 关于xlrd:
Library for developers to extract data from Microsoft Excel (tm) spreadsheet files Extract data from Excel spreadsheets (.xls and .xlsx, versions 2.0 onwards) on any platform. Pure Python (2.6, 2.7, 3.2+). Strong support for Excel dates. Unicode-aware.
翻译过来总结就是:
xlrd 可以在任意平台上读取的excel为: .xls以及 .xlsx 。
xlrd支持和的python版本是: 2.6,2.7 , 3.2+。
2. 关于xlwt:
Library to create spreadsheet files compatible with MS Excel 97/2000/XP/2003 XLS files, on any platform, with Python 2.6, 2.6, 3.3+ This is a library for developers to use to generate spreadsheet files compatible with Microsoft Excel versions 95 to 2003.
翻译过来总结就是:
xlwt支持的excel版本是: Microsoft excel版本 95---2003,也就是 xls文件。
xlwt支持的python版本是:2.6 , 3.3+.
3. 关于xlutils:
This package provides a collection of utilities for working with Excel files. Since these utilities may require either or both of the xlrd and xlwt packages, they are collected together here, separate from either package. Currently available are: xlutils.copy Tools for copying xlrd.Book objects to xlwt.Workbook objects. xlutils.display Utility functions for displaying information about xlrd-related objects in a user-friendly and safe fashion. xlutils.filter A mini framework for splitting and filtering Excel files into new Excel files. xlutils.margins Tools for finding how much of an Excel file contains useful data. xlutils.save Tools for serializing xlrd.Book objects back to Excel files. xlutils.styles Tools for working with formatting information expressed in styles.
翻译过来总结就是:
如果需要在 xlrd以及 xlwt之间进行交互的话,比如拷贝 xlrd 到 xlwt 需要用到xlutils。
目前提供了 copy、display、filter、margins、Save、styles几个函数。
安装 xlrd 和 xlwt
pip install xlrd pip install xlwt pip install xlutils pip list xlrd (1.0.0) xlutils (2.0.0) xlwt (1.1.2)
使用
1. 新建一个excel文件(xlwt)
#coding=\'utf-8\' import xlwt from datetime import datetime def set_style(font_name,font_height,bold=False): style=xlwt.XFStyle() font=xlwt.Font() font.name=font_name # \'Times New Roman\' font.height=font_height font.bold=bold font.colour_index=4 borders=xlwt.Borders() borders.left=6 borders.right=6 borders.top=6 borders.bottom=6 style.font=font style.borders=borders return style def write_to_excel_xlwt(): \'\'\'Write content to a new excel\'\'\' new_workbook=xlwt.Workbook() new_sheet=new_workbook.add_sheet("SheetName_test") new_sheet.write(0,0,"hello") #write cell with style new_sheet.write(0,1,"world",set_style("Times New Roman", 220, True)) style0 = xlwt.easyxf(\'font: name Times New Roman, color-index red, bold on\',num_format_str=\'#,##0.00\') style1 = xlwt.easyxf(num_format_str=\'D-MMM-YY\') new_sheet.write(1, 0, 1234.56, style0) new_sheet.write(1, 1, datetime.now(), style1) #write cell with formula new_sheet.write(2,0,5) new_sheet.write(2,1,8) new_sheet.write(3,0, xlwt.Formula("A3+B3")) new_workbook.save(r"NewCreateWorkbook.xls") #if change to xlsx,then open failed if __name__=="__main__": write_to_excel_xlwt()
代码执行之后,在当前路径下生成excel文件 “NewCreateWorkbook.xls”。内容如下 :
2. 读取excel文件(xlrd)
#coding=\'utf-8\' import xlrd def read_excel_xlrd(): \'\'\'Read Excel with xlrd\'\'\' #file TC_workbook=xlrd.open_workbook(r"NewCreateWorkbook.xls") #sheet all_sheets_list=TC_workbook.sheet_names() print("All sheets name in File:",all_sheets_list) first_sheet=TC_workbook.sheet_by_index(0) print("First sheet Name:",first_sheet.name) print("First sheet Rows:",first_sheet.nrows) print("First sheet Cols:",first_sheet.ncols) second_sheet=TC_workbook.sheet_by_name("SheetName_test") print("Second sheet Rows:",second_sheet.nrows) print("Second sheet Cols:",second_sheet.ncols) first_row=first_sheet.row_values(0) print("First row:",first_row) first_col=first_sheet.col_values(0) print("First Column:",first_col) # cell cell_value=first_sheet.cell(1,0).value print("The 1th method to get Cell value of row 2 & col 1:",cell_value) cell_value2=first_sheet.row(1)[0].value print("The 2th method to get Cell value of row 2 & col 1:",cell_value2) cell_value3=first_sheet.col(0)[1].value print("The 3th method to get Cell value of row 2 & col 1:",cell_value3) if __name__=="__main__": read_excel_xlrd()
运行之后,控制台输出如下 :
All sheets name in File: [\'SheetName_test\'] First sheet Name: SheetName_test First sheet Rows: 4 First sheet Cols: 2 Second sheet Rows: 4 Second sheet Cols: 2 First row: [\'hello\', \'world\'] First Column: [\'hello\', 1234.56, 5.0, \'\'] The 1th method to get Cell value of row 2 & col 1: 1234.56 The 2th method to get Cell value of row 2 & col 1: 1234.56 The 3th method to get Cell value of row 2 & col 1: 1234.56
3. 向已经存在的excel写入(xlrd&xlwt&xlutils)
#coding=\'utf-8\' import xlrd import xlwt from xlutils.copy import copy def write_to_existed_file(): \'\'\'Write content to existed excel file with xlrd&xlutils&xlwt\'\'\' rb = xlrd.open_workbook(r"NewCreateWorkbook.xls",formatting_info=True) wb = copy(rb) ws = wb.get_sheet(0) font=xlwt.Font() font.name="Times New Roman" font.height=220 font.bold=False borders = xlwt.Borders() borders.left = xlwt.Borders.THIN borders.right = xlwt.Borders.THIN borders.top = xlwt.Borders.THIN borders.bottom = xlwt.Borders.THIN pattern = xlwt.Pattern() pattern.pattern = xlwt.Pattern.SOLID_PATTERN pattern.pattern_fore_colour = 2 cell_style = xlwt.XFStyle() cell_style.font = font cell_style.borders = borders cell_style.pattern = pattern ws.write(6,7,"hello world",cell_style) wb.save(r"NewCreateWorkbook.xls") if __name__=="__main__": write_to_existed_file()
运行
# encoding = utf-8
import re,tim
import cx_Oracle as oracle
import numpy as np
import random,os,math
import pandas as pd
import matplotlib.pyplot as plt
from datetime import datetime,timedelta
# import cx_Oracle as oracle# import os# import matplotlib.pyplot as plt
# import cx_Oracle as oracle#panda03_cxOracle2016.pycxOracle2017pandas
print(11111111111111111111111111)
#
#
# def xlrdwrite5文本居中():
# import xlrd,xlwt
# from xlutils.copy import copy # 导入copy模块
# file1path = r\'D:\\note202003春雷log4mail801my200330\\aa1.xls\'
# file2path = r\'D:\\note202003春雷log4mail801my200330\\aa2.xls\'
# # b = xlrd.open_workbook(file1path)
# owb = xlrd.open_workbook(file1path, formatting_info=True)
# count=len(owb.sheets())
# i=0
# for sheet in owb.sheets():
# print (sheet.name,\'--\',i)
# i=i+1
# ws1 = owb.sheet_by_name(\'日通报表\')
# print(\'21ws1-\', ws1, \'-ws1.name-\', ws1.name)
#
# wb = copy(owb) # 利用xlutils.copy下的copy函数复制
# ws = wb.get_sheet(0) # 获取表单0# ws1 = wb.get_sheet(1) wb.sheet_by_name(\'日通报表\')#
# ws.write(3, 4, label=\'444\')
# ws.write(3, 5, (\'555\'+\' \'))# 增加(8,0)的值
# ws.write(3, 6, \'增加\')
# ws.write(3, 7, \'ok\')
# ws.write(3, 8, \'ok\')
# style = xlwt.XFStyle() # 创建一个样式对象,初始化样式
# # fnt = xlwt.Font() # 创建一个文本格式,包括字体、字号和颜色样式特性
# al = xlwt.Alignment() # al = xlwt.Font() #al = xlwt.Alignment()
# al.horz = 0x02 # 设置水平居中
# al.vert = 0x01 # 设置垂直居中
#
# style.alignment = al # sheet.write(0, 0, \'文本居中\', style)
# ws.write(3, 9, str(\'9991\'), style)
# wb.save(file2path) # wb.save(file2path) #保存文件
# print(\'22ws-\', ws, \'-ws.name-\', ws.name)
#
#
#
def tim1():
import time
localtime=time.strftime(\'%Y%m%d%H%M%S\',time.localtime())
return int(localtime)
def hms1():
import time
localtime=time.strftime(\'%Y%m%d%H%M%S\',time.localtime())
return str(localtime)
def tim():
import time
localtime=time.strftime(\'%H%M%S\',time.localtime())
return int(localtime)
def ymd():
import time
localtime=time.strftime(\'%Y%m%d\',time.localtime())
return int(localtime)
def hms():
import time
localtime=time.strftime(\'%H%M%S\',time.localtime())
return str(localtime)
print(222222222222222222222222222222)
print ( tim() , hms() ,type(tim()) ,type(hms()) )
print(333333333333333333333333333333333)
#
#
# def xlrdwrite5():
# import xlrd,xlwt
# from xlutils.copy import copy # 导入copy模块
# file1path = r\'D:\\note202003春雷log4mail801my200330\\aa1.xls\'
# file2path = r\'D:\\note202003春雷log4mail801my200330\\aa2.xls\'
# # b = xlrd.open_workbook(file1path)
# owb = xlrd.open_workbook(file1path, formatting_info=True)
# count=len(owb.sheets())
# i=0
# for sheet in owb.sheets():
# print (sheet.name,\'--\',i)
# i=i+1
# ws1 = owb.sheet_by_name(\'日通报表\')
# print(\'21ws1-\', ws1, \'-ws1.name-\', ws1.name)
#
# wb = copy(owb) # 利用xlutils.copy下的copy函数复制
# ws = wb.get_sheet(0) # 获取表单0# ws1 = wb.get_sheet(1) wb.sheet_by_name(\'日通报表\')#
# ws.write(3, 4, label=\'444\')
# ws.write(3, 5, (\'555\'+\' \'))# 增加(8,0)的值
# ws.write(3, 6, \'增加\')
# ws.write(3, 7, \'ok\')
# ws.write(3, 8, \'ok\')
# style = xlwt.XFStyle() # 创建一个样式对象,初始化样式
# # fnt = xlwt.Font() # 创建一个文本格式,包括字体、字号和颜色样式特性
# al = xlwt.Alignment() # al = xlwt.Font() #al = xlwt.Alignment()
# al.horz = 0x02 # 设置水平居中
# al.vert = 0x01 # 设置垂直居中
# style.alignment = al # sheet.write(0, 0, \'文本居中\', style)
# ws.write(3, 9, \'999\', style)
#
# styl2 = xlwt.XFStyle()
# fnt = xlwt.Font() # 创建一个文本格式,包括字体、字号和颜色样式特性
# fnt.name = u\'微软雅黑\' # 设置其字体为微软雅黑
# fnt.colour_index = 0 # 设置其字体颜色
# fnt.bold = True
# styl2.font = fnt # 将赋值好的模式参数导入Style
# bolder=xlwt.Borders();
# bolder.left=xlwt.Borders.THIN
# bolder.right=xlwt.Borders.THIN
# bolder.top=xlwt.Borders.THIN
# bolder.bottom=xlwt.Borders.THIN
# styl2.borders = bolder
# ws.write(4, 4, \'4444\' , styl2)
#
# styl2 = xlwt.XFStyle()
# fnt = xlwt.Font() # 创建一个文本格式,包括字体、字号和颜色样式特性
# fnt.name = u\'微软雅黑\' # 设置其字体为微软雅黑
# fnt.colour_index = 0 # 设置其字体颜色
# fnt.bold = True
# styl2.font = fnt # 将赋值好的模式参数导入Style
# bolder=xlwt.Borders();
# bolder.left=xlwt.Borders.THIN
# bolder.right=xlwt.Borders.THIN
# bolder.top=xlwt.Borders.THIN
# bolder.bottom=xlwt.Borders.THIN
# styl2.borders = bolder
# ws.write(4, 5, r\'微软雅黑\', styl2)
#
# styl2 = xlwt.XFStyle()
# weizhi = xlwt.Alignment() # al = xlwt.Font() #al = xlwt.Alignment()
# weizhi.horz = 0x02 # 设置水平居中
# weizhi.vert = 0x01 # 设置垂直居中
# styl2.alignment = weizhi # sheet.write(0, 0, \'文本居中\', style)
# fnt = xlwt.Font() # 创建一个文本格式,包括字体、字号和颜色样式特性
# fnt.name = u\'隶书\' # 设置其字体为微软雅黑
# fnt.colour_index = 0 # 设置其字体颜色
# fnt.bold = True
# styl2.font = fnt # 将赋值好的模式参数导入Style
# bolder=xlwt.Borders();
# bolder.left=xlwt.Borders.THIN
# bolder.right=xlwt.Borders.THIN
# bolder.top=xlwt.Borders.THIN
# bolder.bottom=xlwt.Borders.THIN
# styl2.borders = bolder
# ws.write(4, 6, u\'隶6\', styl2)
#
# bolder=xlwt.Borders();
# bolder.left=xlwt.Borders.THIN
# bolder.right=xlwt.Borders.THIN
# bolder.top=xlwt.Borders.THIN
# bolder.bottom=xlwt.Borders.THIN
# styl2.borders = bolder
# styl2 = xlwt.XFStyle()
# weizhi = xlwt.Alignment() # al = xlwt.Font() #al = xlwt.Alignment()
# weizhi.horz = 0x02 # 设置水平居中
# weizhi.vert = 0x01 # 设置垂直居中
# styl2.alignment = weizhi # sheet.write(0, 0, \'文本居中\', style)
# fnt = xlwt.Font() # 创建一个文本格式,包括字体、字号和颜色样式特性
# fnt.name = u\'隶书\' # 设置其字体为微软雅黑
# fnt.colour_index = 0 # 设置其字体颜色
# fnt.bold = False
# styl2.borders=bolder
# styl2.font = fnt # 将赋值好的模式参数导入Style
# ws.write(4, 7, 777.66, styl2)
# #ws.write(4, 7, u\'77\', styl2)
#
# ws.col(0).width = 1222
# ws.col(1).width = 2333
# ws.col(2).width = 5777
# wb.save(file2path) # wb.save(file2path) #保存文件
# print(\'22ws-\', ws, \'-ws.name-\', ws.name)
#
# xlrdwrite5()
print(tim(),4444444444444444444444444444444)
def style12():
import xlrd,xlwt
styl2 = xlwt.XFStyle()
bolder=xlwt.Borders();
bolder.left=xlwt.Borders.THIN
bolder.right=xlwt.Borders.THIN
bolder.top=xlwt.Borders.THIN
bolder.bottom=xlwt.Borders.THIN
styl2.borders = bolder
styl2 = xlwt.XFStyle()
weizhi = xlwt.Alignment() # al = xlwt.Font() #al = xlwt.Alignment()
weizhi.horz = 0x02 # 设置水平居中
weizhi.vert = 0x01 # 设置垂直居中
styl2.alignment = weizhi # sheet.write(0, 0, \'文本居中\', style)
fnt = xlwt.Font() # 创建一个文本格式,包括字体、字号和颜色样式特性
fnt.name = u\'隶书\' # 设置其字体为微软雅黑 # 设置其字体为微软雅黑fnt.name = u\'隶书\'
fnt.colour_index = 0 # 设置其字体颜色
fnt.bold = False
styl2.borders=bolder
styl2.font = fnt # 将赋值好的模式参数导入Style
return styl2
def style11():
import xlrd,xlwt
styl2 = xlwt.XFStyle()
bolder=xlwt.Borders();
bolder.left=xlwt.Borders.THIN
bolder.right=xlwt.Borders.THIN
bolder.top=xlwt.Borders.THIN
bolder.bottom=xlwt.Borders.THIN
styl2.borders = bolder
styl2 = xlwt.XFStyle()
weizhi = xlwt.Alignment() # al = xlwt.Font() #al = xlwt.Alignment()
weizhi.horz = 0x02 # 设置水平居中
weizhi.vert = 0x01 # 设置垂直居中
styl2.alignment = weizhi # sheet.write(0, 0, \'文本居中\', style)
fnt = xlwt.Font() # 创建一个文本格式,包括字体、字号和颜色样式特性
fnt.name = u\'隶书\' # 设置其字体为微软雅黑 # 设置其字体为微软雅黑fnt.name = u\'隶书\'
fnt.colour_index = 0 # 设置其字体颜色
fnt.bold = True
styl2.borders=bolder
styl2.font = fnt # 将赋值好的模式参数导入Style
return styl2
def style22():
import xlrd,xlwt
styl2 = xlwt.XFStyle()
bolder=xlwt.Borders();
bolder.left=xlwt.Borders.THIN
bolder.right=xlwt.Borders.THIN
bolder.top=xlwt.Borders.THIN
bolder.bottom=xlwt.Borders.THIN
styl2.borders = bolder
styl2 = xlwt.XFStyle()
weizhi = xlwt.Alignment() # al = xlwt.Font() #al = xlwt.Alignment()
weizhi.horz = 0x02 # 设置水平居中
weizhi.vert = 0x01 # 设置垂直居中
styl2.alignment = weizhi # sheet.write(0, 0, \'文本居中\', style)
fnt = xlwt.Font() # 创建一个文本格式,包括字体、字号和颜色样式特性
fnt.name = u\'微软雅黑\' # 设置其字体为微软雅黑fnt.name = u\'微软雅黑\' # 设置其字体为微软雅黑fnt.name = u\'隶书\' # 设置其字体为微软雅黑
fnt.colour_index = 0 # 设置其字体颜色
fnt.bold = False
styl2.borders=bolder
styl2.font = fnt # 将赋值好的模式参数导入Style
return styl2
def style21():
import xlrd,xlwt
styl2 = xlwt.XFStyle()
bolder=xlwt.Borders();
bolder.left=xlwt.Borders.THIN
bolder.right=xlwt.Borders.THIN
bolder.top=xlwt.Borders.THIN
bolder.bottom=xlwt.Borders.THIN
styl2.borders = bolder
styl2 = xlwt.XFStyle()
weizhi = xlwt.Alignment() # al = xlwt.Font() #al = xlwt.Alignment()
weizhi.horz = 0x02 # 设置水平居中
weizhi.vert = 0x01 # 设置垂直居中
styl2.alignment = weizhi # sheet.write(0, 0, \'文本居中\', style)
fnt = xlwt.Font() # 创建一个文本格式,包括字体、字号和颜色样式特性
fnt.name = u\'微软雅黑\' # 设置其字体为微软雅黑fnt.name = u\'微软雅黑\' # 设置其字体为微软雅黑fnt.name = u\'隶书\' # 设置其字体为微软雅黑
fnt.colour_index = 0 # 设置其字体颜色
fnt.bold = True
styl2.borders=bolder
styl2.font = fnt # 将赋值好的模式参数导入Style
return styl2
def xlrdwrite6():
# def xlrdwrite6():可以集成字体
import xlrd,xlwt
from xlutils.copy import copy # 导入copy模块
file1path = r\'D:\\note202003春雷log4mail801my200330\\aa1.xls\'
file2path = r\'D:\\note202003春雷log4mail801my200330\\aa2.xls\'
# b = xlrd.open_workbook(file1path)
owb = xlrd.open_workbook(file1path, formatting_info=True)
count=len(owb.sheets())
i=0
for sheet in owb.sheets():
print (sheet.name,\'--\',i)
i=i+1
ws1 = owb.sheet_by_name(\'日通报表\')
print(\'21ws1-\', ws1, \'-ws1.name-\', ws1.name)
wb = copy(owb) # 利用xlutils.copy下的copy函数复制
ws = wb.get_sheet(0) # 获取表单0# ws1 = wb.get_sheet(1) wb.sheet_by_name(\'日通报表\')#
ws.write(3, 4, label=\'444\')
ws.write(3, 5, (\'555\'+\' \'))# 增加(8,0)的值
ws.write(3, 6, \'增加\')
ws.write(3, 7, \'ok\')
ws.write(3, 8, \'ok\')
style = xlwt.XFStyle() # 创建一个样式对象,初始化样式
# fnt = xlwt.Font() # 创建一个文本格式,包括字体、字号和颜色样式特性
al = xlwt.Alignment() # al = xlwt.Font() #al = xlwt.Alignment()
al.horz = 0x02 # 设置水平居中
al.vert = 0x01 # 设置垂直居中
style.alignment = al # sheet.write(0, 0, \'文本居中\', style)
ws.write(3, 9, \'999\', style)
styl2 = xlwt.XFStyle()
fnt = xlwt.Font() # 创建一个文本格式,包括字体、字号和颜色样式特性
fnt.name = u\'微软雅黑\' # 设置其字体为微软雅黑
fnt.colour_index = 0 # 设置其字体颜色
fnt.bold = True
styl2.font = fnt # 将赋值好的模式参数导入Style
bolder=xlwt.Borders();
bolder.left=xlwt.Borders.THIN
bolder.right=xlwt.Borders.THIN
bolder.top=xlwt.Borders.THIN
bolder.bottom=xlwt.Borders.THIN
styl2.borders = bolder
ws.write(4, 4, \'4444\' , styl2)
styl2 = xlwt.XFStyle()
fnt = xlwt.Font() # 创建一个文本格式,包括字体、字号和颜色样式特性
fnt.name = u\'微软雅黑\' # 设置其字体为微软雅黑
fnt.colour_index = 0 # 设置其字体颜色
fnt.bold = True
styl2.font = fnt # 将赋值好的模式参数导入Style
bolder=xlwt.Borders();
bolder.left=xlwt.Borders.THIN
bolder.right=xlwt.Borders.THIN
bolder.top=xlwt.Borders.THIN
bolder.bottom=xlwt.Borders.THIN
styl2.borders = bolder
ws.write(4, 5, r\'微软雅黑\', styl2)
styl2 = xlwt.XFStyle()
weizhi = xlwt.Alignment() # al = xlwt.Font() #al = xlwt.Alignment()
weizhi.horz = 0x02 # 设置水平居中
weizhi.vert = 0x01 # 设置垂直居中
styl2.alignment = weizhi # sheet.write(0, 0, \'文本居中\', style)
fnt = xlwt.Font() # 创建一个文本格式,包括字体、字号和颜色样式特性
fnt.name = u\'隶书\' # 设置其字体为微软雅黑
fnt.colour_index = 0 # 设置其字体颜色
fnt.bold = True
styl2.font = fnt # 将赋值好的模式参数导入Style
bolder=xlwt.Borders();
bolder.left=xlwt.Borders.THIN
bolder.right=xlwt.Borders.THIN
bolder.top=xlwt.Borders.THIN
bolder.bottom=xlwt.Borders.THIN
styl2.borders = bolder
ws.write(4, 6, u\'隶6\', styl2)
sty11=style12()
ws.write(4, 7, tim(), sty11)
sty10=style11()
ws.write(4, 8, tim(), sty10)
sty11=style12()
ws.write(5, 7, hms(), sty11)
sty10=style11()
ws.write(5, 8, hms(), sty10)
#ws.write(4, 7, u\'77\', styl2)
ws.col(0).width = 1680
ws.col(1).width = 2333
ws.col(2).width = 5777
wb.save(file2path) # wb.save(file2path) #保存文件
print(\'22ws-\', ws, \'-ws.name-\', ws.name)
#def xlrdwrite6():可以集成字体
print(tim(),555555555555555555555555555555555555555555555555555555555555555555)
def xlrdwrite7():
import xlrd,xlwt
from xlutils.copy import copy # 导入copy模块
file1path = r\'D:\\note202003春雷log4mail801my200330\\aa1.xls\'
file2path = r\'D:\\note202003春雷log4mail801my200330\\aa2.xls\'
# b = xlrd.open_workbook(file1path)
owb = xlrd.open_workbook(file1path, formatting_info=True)
count=len(owb.sheets())
i=0
for sheet in owb.sheets():
print (sheet.name,\'--\',i)
i=i+1
ws1 = owb.sheet_by_name(\'日通报表\')
print(\'21ws1-\', ws1, \'-ws1.name-\', ws1.name)
wb = copy(owb) # 利用xlutils.copy下的copy函数复制
ws = wb.get_sheet(0) # 获取表单0# ws1 = wb.get_sheet(1) wb.sheet_by_name(\'日通报表\')#
ws.write(3, 4, label=\'444\')
ws.write(3, 5, (\'555\'+\' \'))# 增加(8,0)的值
ws.write(3, 6, \'增加\')
ws.write(3, 7, \'ok\')
ws.write(3, 8, \'ok\')
ws.write(3, 9, \'999\')
ws.write(4, 4, r\'微软雅黑\', style11())
ws.write(4, 5, r\'微软雅黑\', style11())
ws.write(4, 6, u\'隶书666\', style12())
ws.write(4, 7, u\'隶书666\', style12())
ws.write(4, 8, hms(), style21())
ws.write(4, 9, hms(), style22())
#ws.write(4, 7, u\'77\', styl2)
ws.write(5, 4, r\'微软雅黑\', style11())
ws.write(5, 5, r\'微软雅黑\', style11())
ws.write(5, 6, u\'隶书666\', style12())
ws.write(5, 7, u\'隶书666\', style12())
ws.write(5, 8, hms(), style21())
ws.write(5, 9, hms(), style22())
#ws.write(4, 7, u\'77\', styl2)5
ws.write(6, 4, hms(), style22() )
ws.write(6, 5, hms(), style22() )
ws.write(6, 6, hms(), style22() )
ws.write(6, 7, hms(), style22() )
ws.write(6, 8, hms(), style22() )
ws.write(6, 9, hms(), style22() )
#ws.write(4, 7, u\'77\', styl2)5
for i in range(6, 10):
for j in range(5, 9):
ws.write(i, j, (str(i)+str(j)+\' = \'+str(i*j)) , style22())
ws.col(0).width = 1680
ws.col(1).width = 2333
ws.col(2).width = 5777
wb.save(file2path) # wb.save(file2path) #保存文件
print(\'22ws-\', ws, \'-ws.name-\', ws.name)
xlrdwrite7()
print(tim(),666666666666666666666666666666666666666666)
# for i in range(6,10):
# for j in range(5,i):
# print(i,j)
print(tim(),7777777777777777777777777777777777777777777)
#
# import os
# os.system(" taskkill /F /IM cmd* /T")
#
以上是关于xls2- 用Python读写Excel文件-乘法口诀的主要内容,如果未能解决你的问题,请参考以下文章