python操作excel

  长期以来都想用python对Excel进行一些列的操作,但由于某种神秘的力量控制着我,一直未果,今天有幸用requests模块和BeautifulSoup模块进行爬虫练习,拿到了一大批数据,照我以前,都只是用字典啊、列表啊,或者文本文件存放,之前没觉得哪里不好,但今天的我很奇怪,怎么看怎么不爽,而且,如果我把我爬取的数据给别人看的话,确实有点难堪,没有脸给别人看。于是,我拼尽平生所有能力挣脱某种神秘力量的束缚,粗略的学习了下用python如何操作Excel,在此跟大家分享下,鄙人水平有限,不够深入或有问题的地方还请大佬指教。

  一、用xlrd模块对Excel进行读取

import xlrd
#打开Excel文件读取数据 data=xlrd.open_workbook('myexcel.xls')
#获取一个工作表 table = data.sheets()[0] #通过索引顺序获取 table = data.sheet_by_index(0) #通过索引顺序获取 table = data.sheet_by_name(u'Sheet1')#通过名称获取
获取整行或整列的值(数组)
table.row_values(num) table.col_values(num)
获取行数和列数 table.nrows table.ncols
获取单元格 table.cell(num1,num2).value

  二、用xlwt模块对Excel进行写入

  1,基本写入操作

import xlwt
#创建workbook,也就是新建一个Excel文件 workbook=xlwt.Workbook(encoding='ascii')
#在刚才创建的Excel文件下创建一个sheet表 worksheet=workbook.add_sheet('mysheet')
#往单元格里写入数据 worksheet.write(num,num,label='xxxxxx')
#保存Excel文件 workbook.save('myworkbook.xls')

  2,font设置

font = xlwt.Font() # Create the Font  大概就是创建一个font对象,然后对其进行设置
font.name = 'Times New Roman'
font.bold = True
font.underline = True
font.italic = True
style = xlwt.XFStyle() # Create the Style  #创建一个style对象,然后把font对象赋给它
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


下面的就不是很懂,大概也是font对象的一些设置 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 = ?

  3,每列的宽度设置

worksheet.col(0).width = 3333 #这是第0列

  4,往单元格里插入时间

 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)

  5,在单元格插入公式

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])

  6,单元格插连接

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

  7,合并行和列

 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.合并第0列到底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.合并第1行到第2列和第0列到第3列
 workbook.save('Excel_Workbook.xls')

  8,Alignment

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')

  9,Borders

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')

  10,Background Color

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')

  纯属借鉴大佬的笔记,在此感谢大佬

  三、小应用

from bs4 import BeautifulSoup
import requests
import xlwt
import re
headers={
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36'
}
workbook = xlwt.Workbook(encoding='ascii')
worksheet = workbook.add_sheet('My Worksheet')
worksheet.col(0).width = 14000
worksheet.col(5).width = 24000
url='https://sz.58.com/ershoufang/?utm_source=market&spm=u-2d2yxv86y3v43nkddh1.BDPCPZ_BT&PGTID=0d30000c-0000-4591-0324-370565eccba8&ClickID=1'
res=requests.get(url=url,headers=headers)
con=res.text
soup=BeautifulSoup(con,'lxml')
ss=soup.find('ul',class_='house-list-wrap')
li_list=ss.find_all('li')
patter=re.compile(r'\s',re.S)
for num in range(len(li_list)):
    worksheet.write(num, 0, label=li_list[num].find('a',tongji_label="listclick").text.strip())
    p1=li_list[num].find_all('p')[0]
    span_list=p1.find_all('span')
    worksheet.write(num, 1, label=patter.sub('',span_list[0].text))
    worksheet.write(num, 2, label=patter.sub('',span_list[1].text))
    worksheet.write(num, 3, label=patter.sub('',span_list[2].text))
    worksheet.write(num, 4, label=patter.sub('',span_list[3].text))
    worksheet.write(num,5, label=patter.sub('',li_list[num].find('div',class_='jjrinfo').text))
    worksheet.write(num, 6, label=li_list[num].find('p',class_='sum').text)
    worksheet.write(num, 7, label=li_list[num].find('p',class_='unit').text)
workbook.save('myWorkbook.xls')

python操作excel

上一篇:python数据处理的一些基本操作(二)


下一篇:Content of "Essential Software Test Design"