一、操作word(1)
# 导入库from docx import Documentfrom docx.shared import Ptfrom docx.shared import Inchesfrom docx.oxml.ns import qn# 新建空白文档doc1 = Document()# 新增文档标题doc1.add_heading('如何使用 Python 创建和操作 Word',0)# 保存文件doc1.save('word.docx')
读取word(2)
# 引入库from docx import Document# 打开文档1doc1 = Document('word.docx')# 读取每段内容pl = [ paragraph.text for paragraph in doc1.paragraphs]print('###### 输出word1文章的内容 ######')# 输出读取到的内容for i in pl: print(i)# 打开文档2doc2 = Document('word2.docx')print('\n###### 输出word2文章内容 ######')pl2 = [ paragraph.text for paragraph in doc2.paragraphs]# 输出读取到的内容for j in pl2: print(j)# 读取表格材料,并输出结果tables = [table for table in doc2.tables]for table in tables: for row in table.rows: for cell in row.cells: print (cell.text,end=' ') print() print('\n')
修改word
# 导入库from docx import Documentfrom docx.shared import Ptfrom docx.shared import Inchesfrom docx.oxml.ns import qn# 新建空白文档doc1 = Document()# 新增文档标题doc1.add_heading('如何使用 Python 创建和操作 Word',0)# 创建段落描述doc1.add_paragraph(' Word 文档在我们现在的生活和工作中都用的比较多,我们平时都使用 wps 或者 office 来对 Word 进行处理,可能没想过它可以用 Python 生成,下面我们就介绍具体如何操作……')# 创建一级标题doc1.add_heading('安装 python-docx 库',1)# 创建段落描述doc1.add_paragraph('现在开始我们来介绍如何安装 python-docx 库,具体需要以下两步操作:')# 创建二级标题doc1.add_heading('第一步:安装 Python',2)# 创建段落描述doc1.add_paragraph('在python官网下载python安装包进行安装。')# 创建三级标题doc1.add_heading('第二步:安装 python-docx 库',3)# 创建段落描述doc1.add_paragraph('window下win+R输入CMD打开命令行,输入pip install python-docx即可下载。')# 保存文件doc1.save('word2.docx')
二、Excel表格的创建
# 导入 xlwt 库import xlwt# 创建 xls 文件对象wb = xlwt.Workbook()# 新增两个表单页sh1 = wb.add_sheet('成绩')sh2 = wb.add_sheet('汇总')# 然后按照位置来添加数据,第一个参数是行,第二个参数是列# 写入第一个sheetsh1.write(0, 0, '姓名')sh1.write(0, 1, '专业')sh1.write(0, 2, '科目')sh1.write(0, 3, '成绩')sh1.write(1, 0, '张三')sh1.write(1, 1, '信息与通信工程')sh1.write(1, 2, '数值分析')sh1.write(1, 3, 88)sh1.write(2, 0, '李四')sh1.write(2, 1, '物联网工程')sh1.write(2, 2, '数字信号处理分析')sh1.write(2, 3, 95)sh1.write(3, 0, '王华')sh1.write(3, 1, '电子与通信工程')sh1.write(3, 2, '模糊数学')sh1.write(3, 3, 90)# 写入第二个sheetsh2.write(0, 0, '总分')sh2.write(1, 0, 273)# 最后保存文件即可wb.save('test.xls')
导入Excel表格
# 导入 xlrd 库import xlrd# 打开刚才我们写入的 test_w.xls 文件wb = xlrd.open_workbook("test.xls")# 获取并打印 sheet 数量print( "sheet 数量:", wb.nsheets)# 获取并打印 sheet 名称print( "sheet 名称:", wb.sheet_names())# 根据 sheet 索引获取内容sh1 = wb.sheet_by_index(0)# 或者# 也可根据 sheet 名称获取内容# sh = wb.sheet_by_name('成绩')# 获取并打印该 sheet 行数和列数print( u"sheet %s 共 %d 行 %d 列" % (sh1.name, sh1.nrows, sh1.ncols))# 获取并打印某个单元格的值print( "第一行第二列的值为:", sh1.cell_value(0, 1))# 获取整行或整列的值rows = sh1.row_values(0) # 获取第一行内容cols = sh1.col_values(1) # 获取第二列内容# 打印获取的行列值print( "第一行的值为:", rows)print( "第二列的值为:", cols)# 获取单元格内容的数据类型print( "第二行第一列的值类型为:", sh1.cell(1, 0).ctype)# 遍历所有表单内容for sh in wb.sheets(): for r in range(sh.nrows): # 输出指定行 print( sh.row(r))
修改Excel
# 导入相应模块import xlrdfrom xlutils.copy import copy# 打开 excel 文件readbook = xlrd.open_workbook("test.xls")# 复制一份wb = copy(readbook)# 选取第一个表单sh1 = wb.get_sheet(0)# 在第五行新增写入数据sh1.write(4, 0, '王欢')sh1.write(4, 1, '通信工程')sh1.write(4, 2, '机器学习')sh1.write(4, 3, 89)# 选取第二个表单sh1 = wb.get_sheet(1)# 替换总成绩数据sh1.write(1, 0, 362)# 保存wb.save('test.xls')
三、PFD真不会搞
四、使用面向对象实现一个栈:
class Stack(): def __init__(self): self.stack = [] def push(self,value): self.stack.append(value) return True def pop(self): #先判断栈是否为空 if self.stack: item = self.stack.pop() return item else: return False def top(self): if self.stack: return self.stack[-1] else: return False def length(self): return len(self.stack) def view(self): return ','.join(self.stack)s = Stack()s.push('1')s.push('2')item = s.pop()print(item)print(s.view())最后结果:21
五月八日(附加作业):
使用面向对象操作简单实现队列:
ss Queue(object):def __init__(self,q=[]):self.q=q def inqueue(self,data): self.q.append(data) def popqueue(self): if len(self.q)==0: print '空队列' return ' ' e=self.q[0] del self.q[0] return e queue=Queue()print queue.popqueue() queue.inqueue('a')queue.inqueue('b')queue.inqueue('c')print queue.popqueue() print queue.popqueue() print queue.popqueue() print queue.popqueue()
6,尝试着实现一个链表
单向列表的实现:
class ListNode(object):"""docstring for ListNode"""def __init__(self, val,next = None):self.val = valself.next = nextclass Solution(object):"""从尾到头打印单链表"""def printLinkdeList(self,ListNode):if not listNode:return []result = []while listNode:result.insert(0,listNode.val)listNode = listNode.nextreturn resultif __name__ == '__main__':listNode = Nonefor i in range(3,0,-1):listNode = ListNode(i,listNode)s = Solution()print(s.printLinkdeList(listNode))
注:
参考文献过多,如有发现侵权,请立即联系作者删除谢谢~
创作不易,给孩纸点点赞吧~