温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Python怎么从Excel中计算整理数据并写入Word

发布时间:2022-07-01 09:53:56 来源:亿速云 阅读:403 作者:iii 栏目:开发技术

这篇文章主要介绍了Python怎么从Excel中计算整理数据并写入Word的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Python怎么从Excel中计算整理数据并写入Word文章都会有所收获,下面我们一起来看看吧。

Python实现

首先我们使用Python对该Excel进行解析

from openpyxl import load_workbook
import os
# 获取桌面的路径
def GetDesktopPath():
    return os.path.join(os.path.expanduser("~"), 'Desktop')

path = GetDesktopPath() + '/资料/' # 形成文件夹的路径便后续重复使用
workbook = load_workbook(filename=path + '数据.xlsx')
sheet = workbook.active # 获取当前页
# 可以用代码获取数据范围,如果要批处理循环迭代也方便
# 获取有数据范围
print(sheet.dimensions)
# A1:W10

利用openpyxl读取单元格有以下几种用法

cells = sheet['A1:A4']  # 返回A1-A4的4个单元格
cells = sheet['A'] # 获取A列
cells = sheet['A:C'] # 获取A-C列
cells = sheet[5] # 获取第5行
# 注意如果是上述用cells获取返回的是嵌套元祖
for cell in cells:
    print(cell[0].value) # 遍历cells依然需要取出元祖中元素才可以获取值
# 获取一个范围的所有cell
# 也可以用iter_col返回列
for row in sheet.iter_rows(min_row=1, max_row=3,min_col=2, max_col=4):
    for cell in row:
        print(cell.value)

明白了原理我们就可以解析获取Excel中的数据了

# SQE
SQE = sheet['Q2'].value
# 供应商&制造商
supplier = sheet['G2'].value
# 采购单号
C2_10 = sheet['C2:C10'] # 返回cell.tuple对象
# 利用列表推导式后面同理
vC2_10 = [str(cell[0].value) for cell in C2_10]
# 用set简易去重后用,连接,填word表用
order_num = ','.join(set(vC2_10))
# 用set简易去重后用&连接,word文件名命名使用
order_num_title = '&'.join(set(vC2_10))
# 产品型号
T2_10 = sheet['T2:T10']
vT2_10 = [str(cell[0].value) for cell in T2_10]
ptype = ','.join(set(vT2_10))
# 产品描述
P2_10 = sheet['P2:P10']
vP2_10 = [str(cell[0].value) for cell in P2_10]
info = ','.join(set(vP2_10))
info_title = '&'.join(set(vP2_10))
# 日期
# 用datetime库获取今日时间以及相应格式化
import datetime
today = datetime.datetime.today()
time = today.strftime('%Y年%m月%d日')
# 验货数量
V2_10 = sheet['V2:V10']
vV2_10 = [int(cell[0].value) for cell in V2_10]
total_num = sum(vV2_10) # 计算总数量
# 验货箱数
W2_10 = sheet['W2:W10']
vW2_10 = [int(cell[0].value) for cell in W2_10]
box_num = sum(vW2_10)
# 生成最终需要的word文件名
title = f'{order_num_title}-{supplier}-{total_num}-{info_title}-{time}-验货报告'
print(title)

通过上面的代码,我们就成功的从Excel中提取出来数据,这样Excel部分就结束了,接下来进行word的填表啦,由于这里我们默认读取的word是.docx格式的,实际上读者的需求是.doc格式文件,所以windows用户可以用如下代码批量转化doc,前提是安装好win32com

# pip install pypiwin32
from win32com import client
docx_path = path + '模板.docx'
# doc转docx的函数
def doc2docx(doc_path,docx_path):
    word = client.Dispatch("Word.Application")
    doc = word.Documents.Open(doc_path)
    doc.SaveAs(docx_path, 16)
    doc.Close()
    word.Quit()
    print('\n doc文件已转换为docx \n')
if not os.path.exists(docx_path):
    doc2docx(docx_path[:-1], docx_path)

不过在Mac下暂时没有好的解决策略,如果有思路欢迎交流,好了有docx格式文件后我们继续操作Word部分

docx_path = path + '模板.docx'
from docx import Document
# 实例化
document = Document(docx_path)
# 读取word中的所有表格
tables = document.tables
# print(len(tables))
# 15

确定好每个表格数后即可进行相应的填报操作,table的用法和openpyxl中非常类似,注意索引和原生python一样都是从0开始

tables[0].cell(1, 1).text = SQE
tables[1].cell(1, 1).text = supplier
tables[1].cell(2, 1).text = supplier
tables[1].cell(3, 1).text = ptype
tables[1].cell(4, 1).text = info
tables[1].cell(5, 1).text = order_num
tables[1].cell(7, 1).text = time

我们继续用Python填写下一个表格

for i in range(2, 11):
    tables[6].cell(i, 0).text = str(sheet[f'T{i}'].value)
    tables[6].cell(i, 1).text = str(sheet[f'P{i}'].value)
    tables[6].cell(i, 2).text = str(sheet[f'C{i}'].value)
    tables[6].cell(i, 4).text = str(sheet[f'V{i}'].value)
    tables[6].cell(i, 5).text = str(sheet[f'V{i}'].value)
    tables[6].cell(i, 6).text = '0'
    tables[6].cell(i, 7).text = str(sheet[f'W{i}'].value)
    tables[6].cell(i, 8).text = '0'

tables[6].cell(12, 4).text = str(total_num)
tables[6].cell(12, 5).text = str(total_num)
tables[6].cell(12, 7).text = str(box_num)

这里需要注意两个细节:

  • word写入的数据需是字符串,所以从Excel获取的数据需要用str格式化

  • 表格可能存在合并等其他情况,因此你看到的行数和列数可能不是真实的,需要用代码不断测试。

按照上面的办法,将之前从Excel中取出来的数据一一填充到Word中对应位置就大功告成!最后保存一下即可。

document.save(path + f'{title}.docx')
print('\n文件已生成')

关于“Python怎么从Excel中计算整理数据并写入Word”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“Python怎么从Excel中计算整理数据并写入Word”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI