当前位置:首页 » 编程语言 » pythonxlwt下载

pythonxlwt下载

发布时间: 2023-02-06 03:16:55

1. 如何安装python xlwt

1、首先到下载xlwt-0.7.4.tar.gz;
2、把下载的.gz包解压,如把它解压到F:DOWNLOAD下。
3、下打开cmd,在命令行输入F:,切换到F盘,再cd DOWNLOAD\xlrm-0.7.7,
4、最后 setup.py install.装完了。
应用:
xlwt
import xlwt
filename = xlwt.Workbook ()
sheet = filename.add_sheet('name')
sheet.write(0,0,'hao123')
filename.save('test.xls')

2. Excel用Python读取清洗后怎么写入数据

导入xlrd库。
要导入xlrd库,它是读取excel中数据的库,解压所下载的压缩包,用cmd命令行CD到解压目录,执行pythonsetup.pyinstall命令,要导入xlwt库,它是开发人员用于生成与MicrosoftExcel版本95到2003兼容的电子表格文件的库。接着用cmd命令行切换到下载文件所解压的目录,输入pythonsetup.pyinstall命令,如无意外则安装成功。
openpyxl是用于读取和写入Excel2010xlsx/xlsm/xltx/xltm文件的Python库。

3. python怎么读写excel文件

最近用python处理一个小项目,其中涉及到对excel的读写操作,通过查资料及实践做了一下总结,以便以后用。

python读写excel文件要用到两个库:xlrd和xlwt,首先下载安装这两个库。

1、#读取Excel

importxlrd

data = xlrd.open_workbook(excelFile)

table = data.sheets()[0]

nrows = table.nrows #行数

ncols = table.ncols #列数

for i in xrange(0,nrows):
rowValues= table.row_values(i) #某一行数据
for item in rowValues:
printitem

2、写Excel文件

'''往EXCEl单元格写内容,每次写一行sheet:页签名称;row:行内容列表;rowIndex:行索引;

isBold:true:粗字段,false:普通字体'''

defWriteSheetRow(sheet,rowValueList,rowIndex,isBold):

i = 0

style = xlwt.easyxf('font: bold 1')

#style = xlwt.easyxf('font: bold 0, color red;')#红色字体

#style2 = xlwt.easyxf('pattern: pattern solid, fore_colour yellow; font: bold on;') # 设置Excel单元格的背景色为黄色,字体为粗体

forsvalue inrowValueList:

strValue = unicode(str(svalue),'utf-8')

ifisBold:

sheet.write(rowIndex,i,strValue,style)

else:

sheet.write(rowIndex,i,strValue)

i = i + 1

'''写excel文件'''

defsave_Excel(strFile):

excelFile = unicode(strFile,"utf8")

wbk = xlwt.Workbook()

sheet = wbk.add_sheet('sheet1',cell_overwrite_ok=True)

headList = ['标题1','标题2','标题3','标题4','总计']

rowIndex = 0

WriteSheetRow(sheet,headList,rowIndex,True)

fori inxrange(1,11):

rowIndex = rowIndex + 1

valueList = []

forj inxrange(1,5):

valueList.append(j*i)

WriteSheetRow(sheet,valueList,rowIndex,False)

wbk.save(excelFile)

style2 = xlwt.easyxf('pattern: pattern solid, fore_colour yellow; font: bold on;')

在设置上Excel单元格的背景色时,fore_colour支持的颜色是有限的,仅支持一下颜色

aqua 0x31
black 0x08
blue 0x0C
blue_gray 0x36
bright_green 0x0B
brown 0x3C
coral 0x1D
cyan_ega 0x0F
dark_blue 0x12
dark_blue_ega 0x12
dark_green 0x3A
dark_green_ega 0x11
dark_purple 0x1C
dark_red 0x10
dark_red_ega 0x10
dark_teal 0x38
dark_yellow 0x13
gold 0x33
gray_ega 0x17
gray25 0x16
gray40 0x37
gray50 0x17
gray80 0x3F
green 0x11
ice_blue 0x1F
indigo 0x3E
ivory 0x1A
lavender 0x2E
light_blue 0x30
light_green 0x2A
light_orange 0x34
light_turquoise 0x29
light_yellow 0x2B
lime 0x32
magenta_ega 0x0E
ocean_blue 0x1E
olive_ega 0x13
olive_green 0x3B
orange 0x35
pale_blue 0x2C
periwinkle 0x18
pink 0x0E
plum 0x3D
purple_ega 0x14
red 0x0A
rose 0x2D
sea_green 0x39
silver_ega 0x16
sky_blue 0x28
tan 0x2F
teal 0x15
teal_ega 0x15
turquoise 0x0F
violet 0x14
white 0x09
yellow 0x0D"""

另外一种方式是 用pyExcelerator

4. python卸载后怎么也安装不上

本文主要给大家介绍了关于python卸载再安装遇到问题的解决方法,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。
卸载Python:
先在cmd下查看你的python版本,在网上下载相适应的安装包卸载
如果uninstall卸载失败,先Repair修复再卸载就可以了。
再安装Python遇到问题无法安装解决方法:
安装时遇到verify that you have access to that directory . 用管理员身份运行即可。
pip遇到的问题
如pip install xlwt失败出现You are using pip version 10.0.1, however version 19.1.1 is available.
解决:python -m pip install --upgrade pip
如:如pip install xlwt失败出现Consider using the --user option or check the permissions.
解决:pip install --user xlwt
更多Python相关技术文章,请访问Python教程栏目进行学习!以上就是小编分享的关于python卸载后怎么也安装不上的详细内容希望对大家有所帮助,更多有关python教程请关注环球青藤其它相关文章!

5. 怎么用python读取excel表格的数据

一、读excel表
读excel要用到xlrd模块,官网安装(http://pypi.python.org/pypi/xlrd)。然后就可以跟着里面的例子稍微试一下就知道怎么用了。大概的流程是这样的:
1、导入模块

复制代码代码如下:
import xlrd
2、打开Excel文件读取数据

复制代码代码如下:
data = xlrd.open_workbook('excel.xls')
3、获取一个工作表
① table = data.sheets()[0] #通过索引顺序获取
② table = data.sheet_by_index(0) #通过索引顺序获取
③ table = data.sheet_by_name(u'Sheet1')#通过名称获取
4、获取整行和整列的值(返回数组)

复制代码代码如下:
table.row_values(i)
table.col_values(i)

5、获取行数和列数

复制代码代码如下:
table.nrows
table.ncols

6、获取单元格

复制代码代码如下:
table.cell(0,0).value
table.cell(2,3).value

就我自己使用的时候觉得还是获取cell最有用,这就相当于是给了你一个二维数组,余下你就可以想怎么干就怎么干了。得益于这个十分好用的库代码很是简洁。但是还是有若干坑的存在导致话了一定时间探索。现在列出来供后人参考吧:
1、首先就是我的统计是根据姓名统计各个表中的信息的,但是调试发现不同的表中各个名字貌似不能够匹配,开始怀疑过编码问题,不过后来发现是因为空格。因为在excel中输入的时候很可能会顺手在一些名字后面加上几个空格或是tab键,这样看起来没什么差别,但是程序处理的时候这就是两个完全不同的串了。我的解决方法是给每个获取的字符串都加上strip()处理一下。效果良好
2、还是字符串的匹配,在判断某个单元格中的字符串(中文)是否等于我所给出的的时候发现无法匹配,并且各种unicode也不太奏效,网络过一些解决方案,但是都比较复杂或是没用。最后我采用了一个比较变通的方式:直接从excel中获取我想要的值再进行比较,效果是不错就是通用行不太好,个呢不能问题还没解决。
二、写excel表
写excel表要用到xlwt模块,官网下载(http://pypi.python.org/pypi/xlwt)。大致使用流程如下:
1、导入模块

复制代码代码如下:
import xlwt

2、创建workbook(其实就是excel,后来保存一下就行)

复制代码代码如下:
workbook = xlwt.Workbook(encoding = 'ascii')

3、创建表

复制代码代码如下:
worksheet = workbook.add_sheet('My Worksheet')

4、往单元格内写入内容

复制代码代码如下:
worksheet.write(0, 0, label = 'Row 0, Column 0 Value')

5、保存

复制代码代码如下:
workbook.save('Excel_Workbook.xls')

由于我的需求比较简单,所以这上面没遇到什么问题,唯一的就是建议还是用ascii编码,不然可能会有一些诡异的现象。
当然xlwt功能远远不止这些,他甚至可以设置各种样式之类的。附上一点例子

复制代码代码如下:

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/

6. 怎么用python读取txt文件里指定行的内容,并导入excel

def eachlineof(filename):
''' 逐行读取给定的文本文件,返回行号、剔除末尾空字符的行内容 '''
with open(filename) as handle:
for lno, line in enumerate(handle):
yield lno+1, line.strip()

另外: 读写excel需要第三方类库,可以考虑下载安装xlrd, xlwt

写excel表
写excel表要用到xlwt模块,官网下载(http://pypi.python.org/pypi/xlwt)。大致使用流程如下:
1、导入模块

复制代码代码如下:
import xlwt

2、创建workbook(其实就是excel,后来保存一下就行)

复制代码代码如下:
workbook = xlwt.Workbook(encoding = 'ascii')

3、创建表

复制代码代码如下:
worksheet = workbook.add_sheet('My Worksheet')

4、往单元格内写入内容

复制代码代码如下:
worksheet.write(0, 0, label = 'Row 0, Column 0 Value')

5、保存

复制代码代码如下:
workbook.save('Excel_Workbook.xls')

由于我的需求比较简单,所以这上面没遇到什么问题,唯一的就是建议还是用ascii编码,不然可能会有一些诡异的现象。

当然xlwt功能远远不止这些,他甚至可以设置各种样式之类的。附上一点例子

复制代码代码如下:

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 LibreOffi

7. 如何将一个python中的参数写入excel中

import xlwt
f = xlwt.Workbook() #创建工作薄
sheet1 = f.add_sheet(u'sheet1',cell_overwrite_ok=True) #创建sheet
list = [1,2,3,4,5]
j = 0
for i in list:
sheet1.write(j,0,i) #循环版写入 竖着写
j=j+1
f.save('text.xls')#保存权文件

热点内容
网络访问层 发布:2024-11-01 16:18:38 浏览:349
国产系列ftp 发布:2024-11-01 15:49:53 浏览:702
招编程师傅招聘 发布:2024-11-01 15:34:51 浏览:192
kkt编程 发布:2024-11-01 15:31:26 浏览:614
我的世界管理员进服务器提示 发布:2024-11-01 15:21:12 浏览:126
如何删除qq空间访问记录 发布:2024-11-01 15:20:07 浏览:323
微信上传图片失败 发布:2024-11-01 15:19:53 浏览:117
python网站域名服务器搭建 发布:2024-11-01 15:18:36 浏览:693
量产工具加密 发布:2024-11-01 15:14:31 浏览:674
储备存储 发布:2024-11-01 15:14:28 浏览:330