当前位置: 首页 > news >正文

做详情页的网站在线网页制作工具

做详情页的网站,在线网页制作工具,网站网络推广,搜索 引擎优化需求:修改指定目录下所有文件的页眉页脚,或者往里面添加内容。 1. 这里做了word的实现和excel的实现,如下: 需要先安装 pip3 install pywin32,另外页眉页脚格式设置可以参考: word: 浅谈Wor…

需求:修改指定目录下所有文件的页眉页脚,或者往里面添加内容。

1. 这里做了word的实现和excel的实现,如下:

需要先安装 pip3 install pywin32,另外页眉页脚格式设置可以参考:

word:

浅谈Word.Application,关于js操作word文档的使用_new word.application-CSDN博客

excel:

Python操作Excel教程(图文教程,超详细)Python xlwings模块详解,_xlwings教程-CSDN博客

import os
import xlwings as xw
import win32com.client as win32
import pythoncom#将需要替换页眉页脚的文档放到path下
path = r'C:\Users\d\Desktop\pdf改名脚本\22\2022年\test'
#原始页眉页脚内容
old_name = u'999'
#新页眉页脚内容
new_name = u'888'
#替换失败记录日志
err_log = path + u'\\head修改出错列表.txt'
def log(text):with open( err_log,"a+" ) as f:f.write(text)f.write('\n')def change_headerfooter(path):''' 更改文件的页眉页脚 '''pythoncom.CoInitialize()word = win32.Dispatch('Word.Application')#print(dir(word))word.Visible = 0word.DisplayAlerts = 0exapp = xw.App(visible=False, add_book=False)# excel = win32.Dispatch('Excel.Application')# excel.Visible = 0# excel.DisplayAlerts = 0for parent, dirnames, filenames in os.walk(path):for fn in filenames:filedir = os.path.join(parent, fn)#获取需替换目录下的docx结尾的文档名称if fn.endswith('.docx') or fn.endswith('.doc'):print(filedir)try:doc = word.Documents.Open( filedir )a = word.ActiveDocument.Sectionsn = 0for i in range( len(a) ):#获取当前页眉head = word.ActiveDocument.Sections[i].Headers[0]old_head = str(head)#获取当前页脚foot = word.ActiveDocument.Sections[i].Footers[0]old_foot = str(foot)#print( old_head )#if old_name in old_head:if 1:#old_name存在页眉中时,进行进行替换#用于替换页眉#new_head = old_head.replace( old_name, new_name )#用于补充页眉#new_head = old_head + new_name#print( new_head )word.ActiveDocument.Sections[i].Headers[0].Range.Find.ClearFormatting()word.ActiveDocument.Sections[i].Headers[0].Range.Find.Replacement.ClearFormatting()#页眉重置word.ActiveDocument.Sections[i].Headers[0].Range.Delete()#设置字体大小word.ActiveDocument.Sections[i].Headers[0].Range.Font.Size=20#设置对齐方式,0-左;1-中;2-右word.ActiveDocument.Sections[i].Headers[0].Range.ParagraphFormat.Alignment = 0word.ActiveDocument.Sections[i].Headers[0].Range.InsertAfter(new_name)#替换旧页眉#word.ActiveDocument.Sections[i].Headers[0].Range.Find.Execute( old_head, False, False, False, False, False, False, 1, False, new_name, 2 )#if old_name in old_foot:if 1:##old_name存在页脚中时,进行进行替换#new_foot = old_foot.replace( old_name, new_name )#new_foot = old_foot + new_nameword.ActiveDocument.Sections[i].Footers[0].Range.Find.ClearFormatting()word.ActiveDocument.Sections[i].Footers[0].Range.Find.Replacement.ClearFormatting()#页脚重置word.ActiveDocument.Sections[i].Footers[0].Range.Delete()word.ActiveDocument.Sections[i].Footers[0].Range.InsertAfter(new_name)#替换旧页脚#word.ActiveDocument.Sections[i].Footers[0].Range.Find.Execute( old_foot, False, False, False, False, False, False, 1, False, new_name, 2 )n = n+1doc.Close()except Exception as e:print(e)log(str(filedir))if fn.endswith('.xlsx') or fn.endswith('.xls'):print(filedir)try:ex = exapp.books.open(filedir)#print(dir(ex.sheets))sheet = ex.sheetsprint(sheet[0].name)if 'test' == sheet[0].name:print(sheet.name)else:#如果第一个sheet不是test则加一个test的sheetsheet.add('test',before=sheet[0].name)ex.save()ex.close()except Exception as e:log(str(filedir))word.Quit()exapp.quit()change_headerfooter(path)

补充一下:excel没有用pywin32去读写因为莫名其妙就是卡进程,无法对excel读写,可能和缓存的excel有关,如要编辑3.xlsx,则会卡一个~3.xlsx的进程无法结束。就换成了xlwings去读写。

http://www.hkea.cn/news/936287/

相关文章:

  • 山东做网站的公司seo数据分析哪些方面
  • 做网站用哪几个端口 比较好百度贴吧官网网页
  • 弹幕网站是怎么做的百度在线翻译
  • 独立网站域名代发关键词包收录
  • 如何建立门户网站百度可以发布广告吗
  • 网站开发毕设文献郑州网站优化顾问
  • 青海建设银行的官方网站自己制作网页的网站
  • 网站建设的图片叠加步骤过程2021年搜索引擎排名
  • 网站链接到邮箱怎么做搜索引擎推广试题
  • 十堰城市建设网站制作自己的网站
  • 传媒网站建设公司深圳百度seo优化
  • 公司需要网站 该怎么做公司推广咨询
  • 高中男女做那个视频网站seo做的好的网站
  • 网站建设 摄影服务上海seo网站排名优化公司
  • 网页设计中html代码seo博客网站
  • 怎样建设小游戏网站google关键词搜索技巧
  • 淘宝 客要推广网站怎么做全媒体广告代理
  • 那个b2b网站可以做外贸武汉全网推广
  • 深圳css3网站开发多少钱如何策划一个营销方案
  • 织梦统计网站访问量代码网络营销公司如何建立
  • 外贸营销型网站建设今日最新重大新闻
  • 个性化定制网站长春网络推广优化
  • 合肥庐阳区疫情最新消息seo优化首页
  • h5网站制作接单最新中高风险地区名单
  • 北京市住房城乡建设委网站公司怎么在网上推广
  • 网站建设首页怎样插入视频百度指数在线查询小程序
  • 青州网站制作哪家好aso优化哪家好
  • wordpress做网站优点郑州网站seo优化
  • 宝安做棋牌网站建设找哪家公司好湖南长沙疫情最新消息
  • 四川专业网站建设中国十大企业培训机构排名