下面的语句是我工作中常用到的一些语句使用,可能比较简单,部分参考了网上的代码,just for note
零、参考链接
https://blog.csdn.net/yingshukun/article/details/53470424
https://www.runoob.com/python/python-files-io.html
一、操作excel表格
from openpyxl import Workbook
from openpyxl import load_workbook
#初始化操作
wb = load_workbook("xxx.xlsx")
#wb = openpyxl.Workbook()
#mySheet = wb.create_sheet(index=0, title="sheet1")
#获取所有表格的名字
sheets = wb.sheetnames
#选择第二个表格:
sheets_first = sheets[1]
#获取特定的worksheet
ws = wb[sheets_first]
rows = ws.rows
columns = ws.columns
#获取当前空行
def get_current_row():
for per_row in range(1,1024):
if ws.cell(row=per_row,column=1).value == None:
current_row = per_row
return current_row
#向单元格写入内容
ws.cell(row=current_row, column=1).value = '1'
ws.cell(row=current_row, column=2).value = '2'
ws.cell(row=current_row, column=3).value = '3'
wb.save("xxx.xlsx")
#查看所有行的数据
def read_every():
for row in rows:
line = [col.value for col in row]
print(line)
二、操作word中的表格
from docx import Document
document = Document("xxx.docx")
#获取word中的表格
tables = document.tables
table = tables[0]
document.save("xxx.docx")
三、获取文件md5
import hashlib
def GetFileMd5(filename):
if not os.path.isfile(filename):
return
myhash = hashlib.md5()
f = open(filename,'rb')
while True:
b = f.read(8096)
if not b:
break
myhash.update(b)
f.close()
return myhash.hexdig