1.创建工作簿和工作表
workbook=openpyxl.Workbook() #创建工作簿
sheet=workbook.create_sheet() #创建工作表
2.向某个格子写入内容
sheet[‘A1’]=‘hello,Python’
3.用某个格子写入内容
cell.value=‘hello,Python’
4.使用Python列表数据插入一行
sheet.append(Python列表)
5.插入公式
直接赋值公式字符串
6.查看python可以用哪些公式
from openpyxl.utils import FORMULAE print(FORMULAE)
---------------------------------------------------------------------------------------------------------------------------------------------------------
#创建工作簿和工作表 向某个格子写入内容 用某个格子写入内容
#coding:utf-8 import openpyxl workbook=openpyxl.Workbook() #创建一个新的工作薄,Excel文件 #创建sheet工作表 sheet=workbook.create_sheet() #创建sheet工作表 sheet['A1']='hello,Python' #向某个格子写入内容 cell=sheet['A2'] #用某个格子写入内容 cell.value='Python' #用某个格子写入内容 workbook.save('新表.xlsx') #保存工作薄
#使用Python列表数据插入一行 或多行数据
#coding:utf-8 import openpyxl workbook=openpyxl.load_workbook('新表.xlsx') #加载工作表 sheet=workbook['Sheet1'] #选需要编辑的工作表 lst=['姓名','分数'] sheet.append(lst) #使用Python列表数据插入一行 stu_lst=[ ['张三',90], ['李四',98], ['王五',100], ['陈六',70] ] for row in stu_lst: sheet.append(row) #使用Python列表数据插入多行 workbook.save('新表.xlsx')
#插入公式
#coding:utf-8 import openpyxl workbook=openpyxl.load_workbook('新表.xlsx') sheet=workbook['Sheet1'] sheet['B9']='=sum(B5:B8)' #加入公式 workbook.save('新表.xlsx')
#6.查看python可以用哪些公式
from openpyxl.utils import FORMULAE print(FORMULAE)
#运行输出 F:\python3\python_3.8.3\python.exe E:/PycharmProjects/pythonProject/demon1/chap5/demo13.py frozenset({'ISBLANK', 'ROMAN', 'AMORDEGRC', 'COUPNUM', 'NPER', 'REPLACE', 'PRICEDISC', 'LOWER', 'FDIST', 'ECMA.CEILING', 'DURATION', 'DSTDEVP', 'HEX2DEC', 'ODDLYIELD', 'INTERCEPT', 'DEVSQ', 'TIME', 'SLOPE', 'GEOMEAN', 'LOOKUP', 'DVAR', 'DELTA', 'SYD', 'IMEXP', 'LENB', 'YIELD', 'NOT', 'SUMXMY2', 'COLUMN', 'ODDLPRICE', 'INT', 'TAN', 'CUBEKPIMEMBER', 'COMPLEX', 'ISLOGICAL', 'TANH', 'CUMPRINC', 'SMALL', 'IMLOG10', 'EXPONDIST', 'IMREAL', 'BETADIST', 'HYPERLINK', 'GAMMADIST', 'WEEKNUM', 'CONCATENATE', 'FISHER', 'MID', 'CELL', 'GAMMAINV', 'SIGN', 'NEGBINOMDIST', 'GESTEP', 'ASIN', 'MAX', 'COSH', 'LINEST', 'COMBIN', 'SUM', 'BAHTTEXT', 'CUMIPMT', 'INFO', 'VARP', 'INDEX', 'MIDB', 'ERF', 'MDETERM', 'RIGHT', 'COVAR', 'FINV', 'DEGREES', 'DOLLARDE', 'AVERAGEIFS', 'AMORLINC', 'KURT', 'PRICEMAT', 'DISC', 'TBILLYIELD', 'FISHERINV', 'VALUE', 'PI', 'ISNONTEXT', 'STDEVPA STEYX', 'INDIRECT', 'IMLOG2', 'TRIMMEAN', 'SEARCHB', 'HARMEAN', 'OCT2BIN', 'CUBESETCOUNT', 'PRODUCT', 'ODD', 'ISREF', 'BIN2HEX', 'CRITBINOM', 'DB', 'IFERROR', 'MINA', 'ISEVEN', 'YEARFRAC', 'PROPER', 'RIGHTB', 'COUPDAYS', 'LN', 'PRICE', 'ISNA', 'ISNUMBER', 'LOGNORMDIST', 'RAND', 'UPPER', 'IMCOS', 'DSTDEV', 'GROWTH', 'ERFC', 'TBILLPRICE', 'OCT2DEC', 'IMCONJUGATE', 'VDB', 'WEEKDAY', 'SIN', 'XNPV', 'ISERROR', 'TRIM', 'QUARTILE', 'CEILING', 'WORKDAY.INTL', 'IMABS', 'ISPMT', 'CHIDIST', 'IMSUB', 'ROUNDUP', 'ABS', 'LOGINV', 'TYPE', 'MIN', 'LOG', 'DDB', 'SUMPRODUCT', 'ERROR.TYPE', 'NETWORKDAYS.INTL', 'LARGE', 'PV', 'TRUE ADDRESS', 'INTRATE', 'AND', 'NORMINV', 'DATE', 'PHONETIC', 'EFFECT', 'VARPA', 'FACTDOUBLE', 'DPRODUCT', 'DCOUNT', 'IMSUM', 'SECOND', 'GCD', 'IMAGINARY', 'ODDFYIELD', 'BINOMDIST', 'VAR', 'COUPDAYBS', 'ROUND', 'ROUNDDOWN', 'YEAR', 'MINUTE', 'REPLACEB', 'HYPGEOMDIST', 'EOMONTH', 'SINH', 'WEIBULL', 'TREND', 'DAYS360', 'STANDARDIZE', 'DOLLAR', 'ASINH', 'IMSQRT', 'BESSELJ', 'PERMUT', 'ASC', 'EXACT', 'TIMEVALUE', 'LEFT', 'DSUM', 'OFFSET', 'AVERAGEIF', 'BESSELK', 'FALSE', 'PEARSON', 'DOLLARFR', 'LOGEST', 'OCT2HEX', 'YIELDDISC', 'MINVERSE', 'FVSCHEDULE', 'DEC2BIN', 'FREQUENCY', 'EDATE', 'OR', 'SUMSQ', 'FIND', 'DATEVALUE', 'CONVERT', 'DEC2OCT', 'IMDIV', 'NOMINAL', 'FLOOR', 'TRUNC', 'FTEST', 'SLN', 'ISODD', 'PERCENTRANK', 'SKEW', 'ACCRINTM', 'TODAY', 'DMAX', 'COUPNCD', 'WORKDAY ', 'HLOOKUP', 'FV', 'IPMT', 'ACOSH', 'COUNT', 'DAY', 'COUNTIF', 'CUBEVALUE', 'XIRR', 'BESSELY', 'IMPRODUCT', 'SQRT', 'IMSIN', 'QUOTIENT', 'MEDIAN', 'NETWORKDAYS', 'IF', 'GETPIVOTDATA', 'EXP', 'VLOOKUP', 'COS', 'BIN2DEC', 'TRANSPOSE', 'MROUND', 'MODE', 'CUBEMEMBERPROPERTY', 'CLEAN', 'GAMMALN', 'NA', 'T', 'CUBEMEMBER', 'DAVERAGE', 'RECEIVED', 'SUMX2MY2', 'PMT', 'CHOOSE', 'COUNTIFS', 'REPT', 'DVARP', 'BIN2OCT', 'MULTINOMIAL', 'PROB', 'AVEDEV', 'COLUMNS', 'CODE', 'ISTEXT', 'ROWS', 'SUMIF', 'LEN', 'MOD', 'NORMSDIST', 'PPMT', 'LOG10', 'ACOS', 'TEXT', 'RANDBETWEEN', 'FIXED', 'FACT', 'IRR', 'COUNTBLANK', 'SEARCH', 'NOW', 'AVERAGEA', 'HEX2OCT', 'STDEV STDEVA', 'LCM', 'RSQ', 'TTEST', 'ISO.CEILING', 'ISERR', 'CUBERANKEDMEMBER', 'IMARGUMENT', 'VARA', 'ZTEST', 'N', 'CHAR', 'ROW', 'DATEDIF', 'RADIANS', 'POISSON', 'HOUR', 'AREAS', 'BESSELI', 'ATAN', 'RTD', 'BETAINV', 'YIELDMAT', 'DCOUNTA', 'NORMSINV', 'CORREL', 'ACCRINT', 'NORMDIST', 'IMLN', 'LEFTB', 'HEX2BIN', 'MATCH', 'RANK', 'IMPOWER', 'SUBSTITUTE', 'TINV', 'FORECAST', 'MMULT', 'CHIINV', 'MAXA', 'CHITEST', 'JIS', 'TBILLEQ', 'NPV', 'MIRR', 'CONFIDENCE', 'POWER', 'SQRTPI', 'SUMIFS', 'COUPPCD', 'MONTH', 'DMIN', 'EVEN', 'ATAN2', 'COUNTA', 'SERIESSUM', 'PERCENTILE', 'ATANH', 'MDURATION', 'SUMX2PY2', 'AVERAGE', 'COUPDAYSNC', 'DEC2HEX', 'TDIST', 'DGET', 'STDEVP', 'SUBTOTAL', 'ODDFPRICE', 'FINDB', 'RATE', 'CUBESET'}) 进程已结束,退出代码0
标签:sheet,openpyxl,格子,Python,写入,Excel,3.5,workbook From: https://www.cnblogs.com/988MQ/p/16842333.html