penpyxl basic function demo code
Openpyxl basic function demo code
demo code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
summary
description
- openpyxl basic功能练习
- https://openpyxl.readthedocs.io/en/default/usage.html
:REQUIRES:
:TODO:
:AUTHOR: Pengtao.Fan
:ORGANIZATION:
:CONTACT: fanpengtao@gmail.com
:SINCE: Sun Aug 7 21:21:38 2016
:VERSION: 0.1
"""
#===============================================================================
# PROGRAM METADATA
#===============================================================================
__author__ = ''
__contact__ = ''
__copyright__ = ''
__license__ = ''
__date__ = 'Sun Aug 7 21:21:38 2016'
__version__ = '0.1'
#===============================================================================
# IMPORT STATEMENTS
#===============================================================================
#from visual import * # IMPORTS NumPy.*, SciPy.*, and Visual objects (sphere, box, etc.)
#import matplotlib.pyplot as plt # plt.plot(x,y) plt.show()
#from pylab import * # IMPORTS NumPy.*, SciPy.*, and matplotlib.*
#import os # os.walk(basedir) FOR GETTING DIR STRUCTURE
#import pickle # pickle.load(fromfile) pickle.dump(data, tofile)
#from tkFileDialog import askopenfilename, askopenfile
#from collections import namedtuple
#from ctypes import *
#import glob
#import random
#import cv2
from openpyxl import Workbook
from openpyxl.compat import range
from openpyxl.cell import get_column_letter
from openpyxl import load_workbook
from openpyxl.drawing.image import Image
from openpyxl.utils import FORMULAE #to check the name of a formula
import pprint
import datetime
#===============================================================================
# METHODS
#===============================================================================
class OpenpyxlStudy():
def __init_(self):
pass
def getxl(self):
wb = load_workbook(filename = 'test.xlsx')
pprint.pprint(wb.get_sheet_names())
sheet_ranges = wb['ov']
#f14 = sheet_ranges['F14'] #<Cell ov.F14>
f14 = sheet_ranges['F14'].value #DEE
#f14 = sheet_ranges.cell('F14').value
#f14 = sheet_ranges.cell(raw = 15, column = 14)
print(f14)
def setxl(self):
wb = Workbook()
dest_filename = 'empty_book.xlsx'
ws1 = wb.active #设置下次打开时的活动窗格
ws1.title = "range names"
for row in range(1, 40):
ws1.append(range(600))
ws2 = wb.create_sheet(title="Pi")
ws2['F5'] = 3.14
ws3 = wb.create_sheet(title="Data")
for row in range(10, 20):
for col in range(27, 54):
#这种cell赋值方法比较新
#注意get_column_letter
_ = ws3.cell(column=col, row=row, value="%s" % get_column_letter(col))
print(ws3['AA10'].value)
wb.save(filename = dest_filename)
def Fommats(self):
'''Using number formats'''
#dest_filename = 'empty_book.xlsx'
#wb = load_workbook(filename = dest_filename)
wb = Workbook(guess_types=True)
ws = wb.active
# set data using a Python datetime
ws['A1'] = datetime.datetime.now()
print(ws['A1'].number_format)
# set percentage using a string followed by the percent sign
ws['B1'] = '3.14%'
print(ws['B1'].value)
print(ws['B1'].number_format)
wb.save("fommats.xlsx")
def Formulae(self):
'''excel自带数学公式功能'''
wb = Workbook()
ws = wb.active
# 添加excel自带的sum公式
ws['A1'] = '=SUM(1, 2)'
wb.save('formula.xlsx')
if "HEX2DEC" in FORMULAE:
print("HEX2DEC in FORMULAE")
else:
print("HEX2DEC not in FORMULAE")
def MergeCells(self):
'''合并单元格'''
'''Merge / Unmerge cells'''
wb = Workbook()
ws = wb.active #设置下次打开时的活动窗格
ws = wb.create_sheet(title="MergeCells")
ws.merge_cells('A1:B2')
#or
#ws.merge_cells(start_row=0,start_column=0,end_row=8,end_column=8)
ws['A1'] = 'You should see three logos below'
# create an image
#img = Image('image.jpg')
# add to worksheet and anchor next to cells
#ws.add_image(img, 'A1')
wb.save('MergeCells.xlsx')
pass
def UnMergeCells(self):
'''拆分单元格'''
'''Merge / Unmerge cells'''
pass
def InsertImage(self):
'''inset image at A1 cell'''
wb = Workbook()
ws = wb.active #设置下次打开时的活动窗格
ws.title = 'InsertImageSheet'
ws.merge_cells('A1:D7')
ws['A1'] = 'You should see three logos below'
# create an image
img = Image('image.jpg')
# add to worksheet and anchor next to cells
ws.add_image(img, 'A1')
#==============================================================================
# '''虽然A1占用了A3 格子,但不能如下写。'''
# ws2 = wb.create_sheet('SecondImageSheet')
# ws2.merge_cells('A1:D7')
# ws2['A3'] = 'You should see three logos below'
#
# # create an image
# img = Image('image.jpg')
# # add to worksheet and anchor next to cells
# ws.add_image(img, 'A3')
#==============================================================================
wb.save('InsertImage.xlsx')
def Foldcolumns(self):
'''隐藏某些列'''
wb = Workbook(True)
ws = wb.create_sheet()
ws.column_dimensions.group('A','D', hidden=True)
wb.save('Foldcolumns.xlsx')
#===============================================================================
# MAIN METHOD AND TESTING AREA
#===============================================================================
def main():
"""Description of main()"""
test = OpenpyxlStudy()
#test.getxl()
#test.setxl()
#test.Fommats()
#test.Formulae()
#test.MergeCells()
#test.InsertImage()
test.Foldcolumns()
if __name__ == '__main__':
main()
penpyxl basic function demo code的更多相关文章
- RAD Studio Demo Code和几个国外FMX网站 good
FireMonkey X – Amazing overview of FireMonkey FMX Feeds – All your FireMonkey news in one place FMX ...
- ps2keyboard demo code for 8052
#pragma code symbols debug objectextend #include <reg51.h> /* special function register declar ...
- c++ demo code
/* //多继承 #include <iostream> using namespace std; class Sofa { public: Sofa(); ~Sofa(); void s ...
- ( ! ) Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in D:\demo\code\yolo\index\index.php on li
sql语句为:$sql="select count(*) from com where a_id=$v['id']"; 出现以下错误: 原因: 变量没有用花括号引起来 改为: $ ...
- npm install 安装报错:npm ERR! EPERM npm ERR! -4048 npm ERR! Error: EPERM: operation not permitted, unlink 'D:\test\demo\code\materialT\node_modules\.staging'
更新项目依赖包,删除掉package-lock.json.node_modules,运行npm install,报如上错误信息,查询资料说是没有权限,本人用管理员身份打开powershell,运行np ...
- swagger demo code
//Application 开启注解 @EnableSwagger2public class Application { public static void main(String[] args) ...
- 《JavaScript高级程序设计 第3版》-学习笔记-3
P84-P137页, 这一章看的真久,这个月事太多了.有些内容在代码注释里没提出来了 1.JS强大的数组类型,元素类型任意,提供了非常多的操作数组的方法和属性 /* 数组类型 */ //stack v ...
- Delphi xe7 FireMonkey / Mobile (Android, iOS)生成 QR Code完整实例
这个实例在windows.OS X.IOS和Android等平台运行正常.本文参考这个网站提供的方法:http://zarko-gajic.iz.hr/firemonkey-mobile-androi ...
- [Quote]Creating basic Excel workbook with Open XML
Creating basic Excel workbook with Open XML [Quote from]http://www.codeproject.com/Articles/371203/C ...
随机推荐
- 项目vue2.0仿外卖APP(六)
goods 商品列表页开发 布局编写 除了商品之外还有购物车,还有个详情页,挺复杂的. 两栏布局:左侧固定宽度,右侧自适应,还是用flex. 因为内容可能会超过手机高度,超过就隐藏.左右两侧的内容是可 ...
- word20161222
T.120 tag / 标记 TAPI, Telephony API / 电话 API target journaling / 目标日志 taskbar / 任务栏 taskbar button / ...
- (lintcode全部题目解答之)九章算法之算法班题目全解(附容易犯的错误)
--------------------------------------------------------------- 本文使用方法:所有题目,只需要把标题输入lintcode就能找到.主要是 ...
- 最新版 chrome 33中,backgroundPosition 改了.
ctrl.css('backgroundPosition' 返回值不一样, 原来是:-75px 0px 现在是:left 75px top 0px // chrome 33 返回的是 left 0px ...
- Android issues
1. Android studio 2.0 Error:Exception in thread "main" java.lang.UnsupportedClassVersionEr ...
- SAP SMARTFORM 记录实际打印次数
http://blog.csdn.net/wangjolly/article/details/8334008
- Jquary入门( 修改内容)
1. 使用JQ时需要先引用 JQ 包: 其他的JQ代码 需要写在 引用标签的下面如下图[基本格式] JQ中 是纯代码 没有判断 没有循环 如果 有 时间间隔和延迟 则使用JS 代码 详见 下面例 ...
- JavaCV配置
下载javacv-1.2-bin.zip https://github.com/bytedeco/javacv 解压 在Eclipse项目 Referenced Libraries 中 Add Ext ...
- linux通过挂载系统光盘搭建本地yum仓库的方法
1.挂载光盘 [root@localhost ~]# mount /dev/cdrom /media/cdrom/ mount: /dev/sr0 写保护,将以只读方式挂载 /media下的cdrom ...
- php 导出excle的.csv格式的数据时乱码问题
1.header('Content-Encoding: XXXX'); 有可能是编码问题:可以尝试UTF-8,GBK,GB2312,等编码格式 2.有可能是文件编码问题,虽然UTF-8不建议带BOM, ...