openpyxl模块 处理Excel表格
pip install openpyxl
openpyxl.load_workbook() 接受文件
get_sheet_names() 获取工作簿中所有表明的列表
get_sheet_by_name() 传递表名字符串获得
get_active_sheet() 取得工作簿的活动表
cell() 传入row,column 获取单元格
>>>import openpyxl
>>> wb = openpyxl.load_workbook('test.xlsx')
>>>sheet = wb.get_sheet_by_name('Sheet3')
>>> c=sheet['B5']
>>> c.row
5
>>> c.column
2
>>> c.coordinate
'B5'
>>>sheet['A1':'C3'] 取得A1到C3
>>>list(sheet.columns)[1] 获取B列
>>>sheet['B'] 获取B列
>>>list(sheet.rows)[1] 获取第二行
>>>sheet['2'] 获取第二行
max_row 获取最大行
max_column 获取最大列
from openpyxl.utils import get_column_letter,column_index_from_string
get_column_letter(#) 列表数字转换为字母
column_index_from_string() 列表字母转换为数字
新建保存Excel
>>> wb = openpyxl.Workbook()
>>> sheet=wb.get_active_sheet()
>>> sheet.title='my name is test'
>>> sheet['B5']='hello Word'
>>> wb.save('my_name_is_test.xlsx')
设置字体风格
>>> import openpyxl
>>> from openpyxl.styles import Font
>>> wb = openpyxl.Workbook()
>>> sheet = wb.get_sheet_by_name('Sheet')
>>> italic24Font = Font(size=24, italic=True)
>>> styleObj = Style(font=italic24Font)
>>> sheet['A'].style/styleObj
>>> sheet['A1'] = 'Hello world!'
>>> wb.save('styled.xlsx')
公式
>>> import openpyxl
>>> wb = openpyxl.Workbook()
>>> sheet = wb.get_active_sheet()
>>> sheet['A1'] = 200
>>> sheet['A2'] = 300
>>> sheet['A3'] = '=SUM(A1:A2)'
>>> wb.save('writeFormula.xlsx')
调整行高和列宽
>>> import openpyxl
>>> wb = openpyxl.Workbook()
>>> sheet = wb.get_active_sheet()
>>> sheet['A1'] = 'Tall row'
>>> sheet['B2'] = 'Wide column'
>>> sheet.row_dimensions[1].height = 70
>>> sheet.column_dimensions['B'].width = 20
>>> wb.save('dimensions.xlsx')
合并单元格
>>> import openpyxl
>>> wb = openpyxl.Workbook()
>>> sheet = wb.get_active_sheet()
>>> sheet.merge_cells('A1:D3')
>>> sheet['A1'] = 'Twelve cells merged together.'
>>> sheet.merge_cells('C5:D5')
>>> sheet['C5'] = 'Two merged cells.'
>>> wb.save('merged.xlsx')
拆分单元格
>>> import openpyxl
>>> wb = openpyxl.load_workbook('merged.xlsx')
>>> sheet = wb.get_active_sheet()
>>> sheet.unmerge_cells('A1:D3')
>>> sheet.unmerge_cells('C5:D5')
>>> wb.save('merged.xlsx')
冻结窗口
freeze_panes 的设置 冻结的行和列
sheet.freeze_panes = 'A2' 行 1
sheet.freeze_panes = 'B1' 列 A
sheet.freeze_panes = 'C1' 列 A 和列 B
sheet.freeze_panes = 'C2' 行 1 和列 A 和列 B
sheet.freeze_panes = 'A1'或
sheet.freeze_panes = None 没有冻结窗格
>>> import openpyxl
>>> wb = openpyxl.load_workbook('produceSales.xlsx')
>>> sheet = wb.get_active_sheet()
>>> sheet.freeze_panes = 'A2' #将 freeze_panes 属性设置为'A2',行 1 将永远可见
>>> wb.save('freezeExample.xlsx')
图表
openpyxl 支持利用工作表中单元格的数据,创建条形图、折线图、散点图和饼
图。要创建图表,需要做下列事情:
1.从一个矩形区域选择的单元格,创建一个 Reference 对象。
2.通过传入 Reference 对象,创建一个 Series 对象。
3.创建一个 Chart 对象。
4.将 Series 对象添加到 Chart 对象。
5.可选地设置Chart对象的drawing.top、drawing.left、drawing.width和drawing.height变量。
6.将 Chart 对象添加到 Worksheet 对象。
Reference 对象需要一些解释。Reference 对象是通过调用openpyxl.charts. Reference()函数并传入 3 个参数创建的:
1.包含图表数据的 Worksheet 对象。
2.两个整数的元组,代表矩形选择区域的左上角单元格,该区域包含图表数据:元组中第一个整数是行,第二个整数是列。请注意第一行是 1,不是 0。
3.两个整数的元组,代表矩形选择区域的右下角单元格,该区域包含图表数据:元组中第一个整数是行,第二个整数是列。
>>> import openpyxl
>>> wb = openpyxl.Workbook()
>>> sheet = wb.get_active_sheet()
>>> for i in range(1, 11): # create some data in column A
sheet['A' + str(i)] = i
>>> refObj = openpyxl.charts.Reference(sheet, (1, 1), (10, 1))
>>> seriesObj = openpyxl.charts.Series(refObj, title='First series')
>>> chartObj = openpyxl.charts.BarChart()
>>> chartObj.append(seriesObj)
>>> chartObj.drawing.top = 50 # set the position
>>> chartObj.drawing.left = 100
>>> chartObj.drawing.width = 300 # set the size
>>> chartObj.drawing.height = 200
>>> sheet.add_ _chart(chartObj)
>>> wb.save('sampleChart.xlsx')
openpyxl.charts.BarChart() 创建一个条形图
openpyxl.charts.LineChart() 建折线图
openpyxl.charts.ScatterChart() 散点图
openpyxl.charts.PieChart() 饼图
PyPDF2 模块 处理pdf文件
pip install PyPDF2
python-docx 模块 处理Word文档
pip install python-docx
csv 模块 处理csv文件
import csv csvFile = open("../files/test.csv", 'w+') try: writer = csv.writer(csvFile) writer.writerow(('number', 'number plus 2', 'number times 2')) for i in range(10): writer.writerow( (i, i+2, i*2)) finally: csvFile.close()