python常用语句(附带调用google翻译)

这篇博客总结了作者在工作中常用的Python语句,包括操作Excel和Word表格、计算文件MD5、解压ZIP文件、遍历目录、SQLite3数据库操作、系统命令执行、调用Google翻译、I/O操作及文件处理,还介绍了如何处理输入和正则表达式。
摘要由CSDN通过智能技术生成

下面的语句是我工作中常用到的一些语句使用,可能比较简单,部分参考了网上的代码,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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值