Skip to content

Commit

Permalink
更新了基础部分的部分内容
Browse files Browse the repository at this point in the history
  • Loading branch information
jackfrued committed Sep 25, 2018
1 parent 34e5513 commit 76f6568
Show file tree
Hide file tree
Showing 53 changed files with 1,473 additions and 0 deletions.
29 changes: 29 additions & 0 deletions Day01-15/Appendix-A/code/excel1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"""
创建Excel文件
Version: 0.1
Author: 骆昊
Date: 2018-03-26
"""

from openpyxl import Workbook
from openpyxl.worksheet.table import Table, TableStyleInfo

workbook = Workbook()
sheet = workbook.active
data = [
[1001, '白元芳', '男', '13123456789'],
[1002, '白洁', '女', '13233445566']
]
sheet.append(['学号', '姓名', '性别', '电话'])
for row in data:
sheet.append(row)
tab = Table(displayName="Table1", ref="A1:E5")

tab.tableStyleInfo = TableStyleInfo(
name="TableStyleMedium9", showFirstColumn=False,
showLastColumn=False, showRowStripes=True, showColumnStripes=True)
sheet.add_table(tab)
workbook.save('./res/全班学生数据.xlsx')
22 changes: 22 additions & 0 deletions Day01-15/Appendix-A/code/excel2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"""
读取Excel文件
Version: 0.1
Author: 骆昊
Date: 2018-03-26
"""

from openpyxl import load_workbook
from openpyxl import Workbook

workbook = load_workbook('./res/学生明细表.xlsx')
print(workbook.sheetnames)
sheet = workbook[workbook.sheetnames[0]]
print(sheet.title)
for row in range(2, 7):
for col in range(65, 70):
cell_index = chr(col) + str(row)
print(sheet[cell_index].value, end='\t')
print()
11 changes: 11 additions & 0 deletions Day01-15/Appendix-A/code/pdf1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"""
创建PDF文件
Version: 0.1
Author: 骆昊
Date: 2018-03-26
"""

import PyPDF2
20 changes: 20 additions & 0 deletions Day01-15/Appendix-A/code/pdf2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"""
读取PDF文件
Version: 0.1
Author: 骆昊
Date: 2018-03-26
"""

from PyPDF2 import PdfFileReader

with open('./res/Python课程大纲.pdf', 'rb') as f:
reader = PdfFileReader(f, strict=False)
print(reader.numPages)
if reader.isEncrypted:
reader.decrypt('')
current_page = reader.getPage(5)
print(current_page)
print(current_page.extractText())
26 changes: 26 additions & 0 deletions Day01-15/Appendix-A/code/pillow1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""
使用pillow操作图像
Version: 0.1
Author: 骆昊
Date: 2018-03-26
"""

from PIL import Image

img = Image.open('./res/guido.jpg')
print(img.size)
print(img.format)
print(img.format_description)
img.save('./res/guido.png')

img2 = Image.open('./res/guido.png')
img3 = img2.crop((335, 435, 430, 615))
for x in range(4):
for y in range(5):
img2.paste(img3, (95 * y , 180 * x))
img2.resize((img.size[0] // 2, img.size[1] // 2))
img2.rotate(90)
img2.save('./res/guido2.png')
Binary file not shown.
Binary file added Day01-15/Appendix-A/code/res/guido.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Day01-15/Appendix-A/code/res/学生明细表.xlsx
Binary file not shown.
Binary file not shown.
10 changes: 10 additions & 0 deletions Day01-15/Appendix-A/code/word1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"""
创建Word文件
Version: 0.1
Author: 骆昊
Date: 2018-03-26
"""

21 changes: 21 additions & 0 deletions Day01-15/Appendix-A/code/word2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""
读取Word文件
Version: 0.1
Author: 骆昊
Date: 2018-03-26
"""

from docx import Document

doc = Document('./res/用函数还是用复杂的表达式.docx')
print(len(doc.paragraphs))
print(doc.paragraphs[0].text)
# print(doc.paragraphs[1].runs[0].text)

content = []
for para in doc.paragraphs:
content.append(para.text)
print(''.join(content))
44 changes: 44 additions & 0 deletions Day01-15/Appendix-A/图像和办公文档处理.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
## 图像和办公文档处理

用程序来处理图像以及办公文档在实际开发中非常常见,我们可以通过Python生态圈中的第三方模块来完成这些操作。

### 操作图像

#### 计算机图像相关知识

1. 颜色。如果你有使用颜料画画的经历,那么一定知道混合红、黄、蓝三种颜料可以得到其他的颜色,事实上这三种颜色就是被我们称为美术三原色的东西,它们是不能再分解的基本颜色。在计算机中,我们可以将红、绿、蓝三种色光以不同的比例叠加来组合成其他的颜色,因此这三种颜色就是色光三原色,所以我们通常会将一个颜色表示为一个RGB值或RGBA值(其中的A表示Alpha通道,它决定了透过这个图像的像素,也就是透明度)。
2. 像素。对于一个由数字序列表示的图像来说,最小的单位就是图像上单一颜色的小方格,这些小方块都有一个明确的位置和被分配的色彩数值,而这些一小方格的颜色和位置决定了该图像最终呈现出来的样子,它们是不可分割的单位,我们通常称之为像素(pixel)。每一个图像都包含了一定量的像素,这些像素决定图像在屏幕上所呈现的大小。

#### 用Pillow操作图像

- 剪裁图片
- 调整图片大小
- 旋转和翻转
- 操作像素
- 添加水印

### 处理Excel电子表格

Python的OpenPyXL模块让我们可以在Python程序中读取和修改Excel电子表格,LibreOffice Calc和OpenOffice Calc也都能处理Excel的电子表格文件,这就意味着OpenPyXL模块也能处理来自这些应用程序的电子表格。关于OpenPyXL的使用手册和使用文档可以查看它的[官方文档](https://openpyxl.readthedocs.io/en/stable/#)

### 处理Word文档

| 属性 | 描述 |
| ------------- | ---------- |
| bold | 粗体 |
| italic | 斜体 |
| underline | 下划线 |
| strike | 删除线 |
| double_strike | 双删除线 |
| all_caps | 大写首字母 |
| small_caps | 大写首字母 |
| shadow | 带阴影 |
| outline | 轮廓显示 |
| rtl | 从右向左 |
| imprint | 凹嵌页面 |
| emboss | 凸出页面 |


### 处理PDF文档

PDF是Portable Document Format的缩写,使用.pdf作为文件扩展名。
34 changes: 34 additions & 0 deletions Day01-15/Appendix-B/chatclient.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from socket import socket
from threading import Thread


def main():

class RefreshScreenThread(Thread):

def __init__(self, client):
super().__init__()
self._client = client

def run(self):
while running:
data = self._client.recv(1024)
print(data.decode('utf-8'))

nickname = input('请输入你的昵称: ')
myclient = socket()
myclient.connect(('10.7.189.118', 12345))
running = True
RefreshScreenThread(myclient).start()
while running:
content = input('请发言: ')
if content == 'byebye':
myclient.send(content.encode('utf-8'))
running = False
else:
msg = nickname + ': ' + content
myclient.send(msg.encode('utf-8'))


if __name__ == '__main__':
main()
44 changes: 44 additions & 0 deletions Day01-15/Appendix-B/chatserver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from socket import socket
from threading import Thread


def main():

class ClientHandler(Thread):

def __init__(self, client):
super().__init__()
self._client = client

def run(self):
try:
while True:
try:
data = self._client.recv(1024)
if data.decode('utf-8') == 'byebye':
clients.remove(self._client)
self._client.close()
break
else:
for client in clients:
client.send(data)
except Exception as e:
print(e)
clients.remove(self._client)
break
except Exception as e:
print(e)

server = socket()
server.bind(('10.7.189.118', 12345))
server.listen(512)
clients = []
while True:
curr_client, addr = server.accept()
print(addr[0], '连接到服务器.')
clients.append(curr_client)
ClientHandler(curr_client).start()


if __name__ == '__main__':
main()
29 changes: 29 additions & 0 deletions Day01-15/Appendix-B/fileclient.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from socket import socket
from json import loads
from base64 import b64decode


def main():
client = socket()
client.connect(('192.168.1.2', 5566))
# 定义一个保存二进制数据的对象
in_data = bytes()
# 由于不知道服务器发送的数据有多大每次接收1024字节
data = client.recv(1024)
while data:
# 将收到的数据拼接起来
in_data += data
data = client.recv(1024)
# 将收到的二进制数据解码成JSON字符串并转换成字典
# loads函数的作用就是将JSON字符串转成字典对象
my_dict = loads(in_data.decode('utf-8'))
filename = my_dict['filename']
filedata = my_dict['filedata'].encode('utf-8')
with open('/Users/Hao/' + filename, 'wb') as f:
# 将base64格式的数据解码成二进制数据并写入文件
f.write(b64decode(filedata))
print('图片已保存.')


if __name__ == '__main__':
main()
46 changes: 46 additions & 0 deletions Day01-15/Appendix-B/fileserver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from socket import socket, SOCK_STREAM, AF_INET
from base64 import b64encode
from json import dumps
from threading import Thread


def main():

# 自定义线程类
class FileTransferHandler(Thread):

def __init__(self, cclient):
super().__init__()
self.cclient = cclient

def run(self):
my_dict = {}
my_dict['filename'] = 'guido.jpg'
# JSON是纯文本不能携带二进制数据
# 所以图片的二进制数据要处理成base64编码
my_dict['filedata'] = data
# 通过dumps函数将字典处理成JSON字符串
json_str = dumps(my_dict)
# 发送JSON字符串
self.cclient.send(json_str.encode('utf-8'))
self.cclient.close()

# 1.创建套接字对象并指定使用哪种传输服务
server = socket()
# 2.绑定IP地址和端口(区分不同的服务)
server.bind(('192.168.1.2', 5566))
# 3.开启监听 - 监听客户端连接到服务器
server.listen(512)
print('服务器启动开始监听...')
with open('guido.jpg', 'rb') as f:
# 将二进制数据处理成base64再解码成字符串
data = b64encode(f.read()).decode('utf-8')
while True:
client, addr = server.accept()
# 用一个字典(键值对)来保存要发送的各种数据
# 待会可以将字典处理成JSON格式在网络上传递
FileTransferHandler(client).start()


if __name__ == '__main__':
main()
Binary file added Day01-15/Appendix-B/guido.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 33 additions & 0 deletions Day01-15/Appendix-B/mmdownloader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from time import time
from threading import Thread

import requests


class DownloadHanlder(Thread):

def __init__(self, url):
super().__init__()
self.url = url

def run(self):
filename = self.url[self.url.rfind('/') + 1:]
resp = requests.get(self.url)
with open('/Users/Hao/Downloads/' + filename, 'wb') as f:
f.write(resp.content)


def main():
# 通过requests模块的get函数获取网络资源
resp = requests.get(
'http://api.tianapi.com/meinv/?key=772a81a51ae5c780251b1f98ea431b84&num=10')
# 将服务器返回的JSON格式的数据解析为字典
data_model = resp.json()
for mm_dict in data_model['newslist']:
url = mm_dict['picUrl']
# 通过多线程的方式实现图片下载
DownloadHanlder(url).start()


if __name__ == '__main__':
main()
Binary file added Day01-15/Appendix-B/res/TCP-IP-model.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Day01-15/Appendix-B/res/after-browser.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Day01-15/Appendix-B/res/arpanet.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Day01-15/Appendix-B/res/before-browser.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Day01-15/Appendix-B/res/browers.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Day01-15/Appendix-B/res/browser-market-place.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Day01-15/Appendix-B/res/how-data-is-processed.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Day01-15/Appendix-B/res/osi_rm.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Day01-15/Appendix-B/res/osimodel.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Day01-15/Appendix-B/res/tcpipprotocols.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Day01-15/Appendix-B/res/telnet.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 76f6568

Please sign in to comment.