Python办公自动化,有效告别繁琐操作,955不是梦,建议收藏!

sheet2 = allExcel2.get\_sheet(0)

# 写入数据
i = 0
for row_data in list_row_data:
    for j in range(len(row_data)):
        sheet2.write(sheet.nrows + i, j, row_data[j])
    i += 1
# 保存文件,将原文件覆盖
allExcel2.save(file_name)
print('合并完成')

if name == ‘__main__’:
# 设置文件夹路径
# ““为字符串中的特殊字符,加上r后变为原始字符串,则不会对字符串中的”\t”、“\r” 进行字符串转义
file_dir = ‘.\01 报表合并\word’
#模板顶部表头行数,当前行数减1
top = 2
# 设置文件名,用于保存数据
file_name = ‘save_demo.xls’

# 获取文件夹的路径,该路径下的所有文件夹,以及所有文件
root, dirs, files = get\_allfile\_msg(file_dir)
# 拼凑目录路径+文件名,组成文件的路径,用一个列表存储
allFile_url = get\_allfile\_url(root, files)
# have\_title参数默认为True,为True时不读取excel文件的首行
all\_to\_one(root, allFile_url, file_name=file_name, title=None, have_title=True)

![图片](https://img-blog.csdnimg.cn/img_convert/34645fb5050d45affb6d0bf7b76ca703.png)


![图片](https://img-blog.csdnimg.cn/img_convert/430fa487bec08fe621120e91e911932f.png)


### 批量word转pdf



import win32com.client
import pythoncom
import os

class Word_2_PDF(object):

def \_\_init\_\_(self, filepath, Debug=False):
    """
    :param filepath:
    :param Debug: 控制过程是否可视化
    """
    self.wordApp = win32com.client.Dispatch('word.Application')
    self.wordApp.Visible = Debug
    self.myDoc = self.wordApp.Documents.Open(filepath)

def export\_pdf(self, output_file_path):
    """
    将Word文档转化为PDF文件
    :param output_file_path:
    :return:
    """
    self.myDoc.ExportAsFixedFormat(output_file_path, 17, Item=7, CreateBookmarks=0)

def close(self):
    self.wordApp.Quit()

if name == ‘__main__’:

rootpath = os.getcwd()  # 文件夹路径
save_path = os.getcwd()   # PDF储存位置
pythoncom.CoInitialize()

os_dict = {root:[dirs, files] for root, dirs, files in os.walk(rootpath)}
for parent, dirnames, filenames in os.walk(rootpath):
    for filename in filenames:
        if u'.doc' in filename and u'~$' not in filename:
              # 直接保存为PDF文件
            #print(rootpath+filename)
            a = Word\_2\_PDF(rootpath +'\\'+ filename, True)
            title = filename.split('.')[0]  # 删除.docx
            a.export\_pdf(rootpath  +'\\'+ title+'.pdf')
print('转化完成')

![图片](https://img-blog.csdnimg.cn/img_convert/7fd6852f736391e11d8720b1a8d68c12.png)


### 合同生成



from openpyxl import load_workbook
from docx import Document
from os import listdir
‘’’
定义替换函数
‘’’
def replace_text(old_text, new_text):
#读取所有的自然段
all_paragraphs = document.paragraphs
for paragraph in all_paragraphs:
#循环读取所有的run,并进行新旧文本的替换
for run in paragraph.runs:
run_text = run.text.replace(old_text, new_text)
run.text = run_text
#读取所有的表格
all_tables = document.tables
for table in all_tables:
for row in table.rows:
#循环读取表格中所有的cells,并进行新旧文本的替换
for cell in row.cells:
cell_text = cell.text.replace(old_text, new_text)
cell.text = cell_text
‘’’
获取Excel和Word的文件名
‘’’
for file in listdir():
print(file, ‘listdir’)
if ‘模板.docx’ in file:
docx_name = file
if ‘信息.xlsx’ in file:
xlsx_name = file
‘’’
读取Excel内数据
‘’’
wb = load_workbook(xlsx_name)
sheetx0 = wb.sheetnames
sheetx = wb[sheetx0[0]]

#新文件以第几列数据命名
filename_pos = 1
‘’’
循环读取并替换
‘’’
#合同要素Excel中逐列循环
for row in range(3,sheetx.max_row+1):
document = Document(docx_name)
#openpyxl在使用sheetx.max_column时可能会读取到空的单元格,这里进行剔除
if sheetx.cell(row=row,column=1).value!=None:
#合同要素Excel中逐行循环
for l in range(1,sheetx.max_column+1):
#合同要素Excel中对第一列逐行读取编号
old_text = sheetx.cell(row=1,column=l).value
#合同要素Excel中对循环的当前列逐行读取新要素
new_text = sheetx.cell(row=row,column=l).value
replace_text(str(old_text),str(new_text)) #进行替换
#定义文件名为当前列第一行的内容
filename = str(sheetx.cell(row=row,column=filename_pos).value)
#按定义的文件名进行保存
document.save(“%s.docx”%(filename))
print(‘合同生成完毕!’)


![图片](https://img-blog.csdnimg.cn/img_convert/d2ccf470bf1c8257c0c6eb1e5eed43ee.png)


**「参考链接:」**


https://github.com/jianglin521/python\_office


### 关于Python技术储备


学好 Python 不论是就业还是做副业赚钱都不错,但要学会 Python 还是要有一个学习规划。最后大家分享一份全套的 Python 学习资料,给那些想学习 Python 的小伙伴们一点帮助!


**朋友们如果需要这份完整的资料可以微信扫描下方CSDN官方认证二维码免费领取【保证100%免费】**


![](https://img-blog.csdnimg.cn/img_convert/cdfb1db8995e80eb1256cb92ad659ce9.png)
#### 一、Python学习大纲


Python所有方向的技术点做的整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。  
 ![在这里插入图片描述](https://img-blog.csdnimg.cn/71e2166464ed45959e2863dae1cc4835.jpeg#pic_center)


#### 二、Python必备开发工具


![在这里插入图片描述](https://img-blog.csdnimg.cn/e496e6652efd47f5bbe73ad2ee082d4a.png)


#### 三、入门学习视频


![](https://img-blog.csdnimg.cn/img_convert/e0106a2ebc87d23666cd0a4b476be14d.png)


#### 四、实战案例


光学理论是没用的,要学会跟着一起敲,要动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。![在这里插入图片描述](https://img-blog.csdnimg.cn/7b7d7e133d984b85a09422c3ccfa7396.png)


### 最后

不知道你们用的什么环境,我一般都是用的Python3.6环境和pycharm解释器,没有软件,或者没有资料,没人解答问题,都可以免费领取(包括今天的代码),过几天我还会做个视频教程出来,有需要也可以领取~  

给大家准备的学习资料包括但不限于:  

Python 环境、pycharm编辑器/永久激活/翻译插件  

python 零基础视频教程  

Python 界面开发实战教程  

Python 爬虫实战教程  

Python 数据分析实战教程  

python 游戏开发实战教程  

Python 电子书100本  

Python 学习路线规划

![](https://img-blog.csdnimg.cn/d29631674929476f9c3b30f7ff58dff0.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2ZlaTM0Nzc5NTc5MA==,size_16,color_FFFFFF,t_70)




**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化学习资料的朋友,可以戳这里无偿获取](https://bbs.csdn.net/topics/618317507)**

**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**
资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化学习资料的朋友,可以戳这里无偿获取](https://bbs.csdn.net/topics/618317507)**

**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**
  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值