⽤ptyhon和vba清除Word的标题样式保留字体格式,⽣成标题
⽬录。
不同格式的word⽂档合并为⼀个⽂件。
问题:
⽂件汇编,需要将70多个⽂件汇编成⼀个到⼀个⽂件⾥。最终汇编的⽅式是⽤word--插⼊--对象--⽂件中的⽂字。
但是由于原始⽂件有的设置了⾃动编号。插⼊后在合并后的⽂档上继续编号导致混乱。有的没有设置标题样式,提取不出来⽬录。
最终汇总的时候,需要清楚⾃动编号的域,已经⽣成的编号转换成⽂字,统⼀设置标题格式.
同⼀个段⾥,字体还不⼀样,需要清除Word的标题样式保留字体格式。
解决⽅式:
开始享⽤python+docx库解决。但是docx库在原始⽂档没有style的情况下,⽆论如何添加不进去。
if ,"第*章*"): #匹配模式为问号,及匹配⼀个任意字符
document.add_heading("",level=1)
paragraph.style = document.styles['heading 1']
这样的总是出错,因为heading不存在,查了官⽅⽂档,说word默认有很多样式,但是你不起⽤的话,⽂档⾥是没有这个样式的。docx⽂档⾥也没说怎么解决。
"no style with name 'Heading 1'"
换解决思路:
⽤ win32com 操作
⾥⾯⽤到⼀句
ActiveDocument.ConvertNumbersToText
在VBA⾥运⾏没有问题,但是win32调⽤也出错。
查过程中查到⼀个⽤WIN32直接运⾏宏命令的⽅式,算球,全部代码写在vba⾥,python循环⽬录和重命名就好。这样的好处
是,python只负责循环和重命名,有需求直接修改vba.bas就好了,宏兼容性最⾼。
于是代码如下:
import os
import glob
from win32com.client import constants
from win32com.client import Dispatch #需要安装的是pypiwin32模块
App=Dispatch('Word.Application')
App=Dispatch('Word.Application')
App.Visible=0
def addmaco(file):
App.DisplayAlerts = 0
doc = App.Documents.Open(file)
#导⼊宏代码
#通过⽂档实例,获取VBProject的组件,其中VBComponents中的参数⾄关重要,因为ThisDocument表⽰该⽂档,也就是说所有这篇⽣成⽂档的操作在该组件中都
docCode = doc.VBProject.VBComponents("ThisDocument").CodeModule
macro = ""
#vba.bas为宏⽂件,需要导⼊到ThisDocument,ThisDocument即对应word下,按Alt+F11,调出vba窗⼝,该⽂档下的Microsoft Word对象下的ThisDocument。    string=open("vba.bas", encoding='utf-8')
ad()
docCode.AddFromString(macro)
#doc.Save()  #添加宏这⾥不能保存,因为word⽆法保存为不含宏的⽂件.但是添加宏以后就可以运⾏了。python⾥就是⽤到这个功能
def DelMacro(file): #添加宏
App.DisplayAlerts = 0
doc = App.Documents.Open(file)
doc.Save()
def get_file_path(root_path,file_list,dir_list):
#获取该⽬录下所有的⽂件名称和⽬录名称
dir_or_files = os.listdir(root_path)
for dir_file in dir_or_files:
#获取⽬录或者⽂件的路径
dir_file_path = os.path.join(root_path,dir_file)
#判断该路径为⽂件还是路径
if os.path.isdir(dir_file_path):
dir_list.append(dir_file_path)
#递归获取所有⽂件和⽬录的路径
get_file_path(dir_file_path,file_list,dir_list)
else:
file_list.append(dir_file_path)
root_path = r"D:\企业制度"
file_list = []
dir_list = []
path='d:/'
get_file_path(root_path,file_list,dir_list)
for dirlst in dir_list:
path=dirlst
print(path)
preName="" #⼆级⽬录
dir=(dirlst.split("\\"))
if (len(dir))>2:
preName=(dir[2])
old_names = os.listdir(path)  #取路径下的⽂件名,⽣成列表
for old_name in old_names:      #遍历列表下的⽂件名
if os.path.isfile(path + "\\" +old_name): #防⽌⽂件夹加上.pdf后缀,先判断是否是⽂件,必须带路径
if dswith('.doc') or dswith('.docx'):
file=path + "\\" +old_name
print("打开:"+file)
doc = App.Documents.Open(file)
addmaco(file)
App.Application.Run("numTotext")
filename=App.ActiveDocument.Sentences(1).place('\r','') #删除字符串⾥的回车符
if filename=="": #如果第⼀⾏为空,取第⼆⾏
filename=App.ActiveDocument.Sentences(2).place('\r','')
place('\t','')
place('\t','')
#filenamePara=str(App.ActiveDocument.Paragraphs(1)).replace('\r','')
#place('\t','')
#print(filenamePara)
print(filename)
newpath="d:\\企业制度统⼀格式\\"+preName
Newfile=newpath+"\\"+preName+"_"+str(filename)
print(Newfile)
doc.SaveAs(Newfile,16) #将所有doc,docx都统⼀保存为docx,后⾯带16
doc.Close()
App.Quit()
vba.bas⽂件如下
Sub numTotext()
'数字转⽂本
'仅list是 ActiveDocument.range.listformat.ConvertNumbersToText
ActiveDocument.ConvertNumbersToText '⾃动编号的数字会变成⽂本,但是域还在
'删除第⼀⾏'单位名称'
Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "单位名称^p" '包括换⾏
.
Replacement.Text = ""
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceOne
End With
Selection.HomeKey Unit:=wdStory '回到⾸⾏
Dim searchStr
searchStr = Chr(13)
If (InStr(ActiveDocument.Paragraphs(1).Range.Text, searchStr) = 1) Then    '如果第⼀⾏是回车,删除        Selection.Range.Delete
End If
'去除底纹
Selection.WholeStory
Options.DefaultHighlightColorIndex = wdNoHighlight
Selection.Range.HighlightColorIndex = wdNoHighlight
'Sub 清除Word的标题样式保留字体格式()
getsavefilename
Dim para As Paragraph
Dim fnt As Font
Dim pfmt As Variant
Dim lineup1 As Variant
Dim lineup2 As Variant
Dim linedo1 As Variant
Dim linedo2 As Variant
Dim fntName As Variant
Dim fntsize As Variant
Dim shSj As Variant
Dim lineSpace As Variant
Dim lnSpcrule As Variant
Dim alnMent As Variant
With ActiveDocument.Styles("正⽂").Font
.NameFarEast = "仿宋_GB2312"
.NameAscii = "Calibri"
.NameOther = "Calibri"
.Name = "Calibri"
.Size = 16
End With
'⽆标记
With ActiveWindow.View.RevisionsFilter
.
Markup = wdRevisionsMarkupNone
.View = wdRevisionsViewFinal
End With
'接受修订
ActiveDocument.AcceptAllRevisions
ActiveDocument.TrackRevisions = False
For Each para In ActiveDocument.Paragraphs
With para
'If .Style <> ActiveDocument.Styles("正⽂") 不同的⽂章的正⽂格式也不⼀样,所以⽆论什么格式通通成正⽂            Set fnt = .Range.Font '转换样式之前,先记下来字体
Set pfmt = .Style.ParagraphFormat
fntName = .Range.Font.Name
'MsgBox (.Range.Text)
fntsize = .Range.Font.Size
alnMent = .Range.ParagraphFormat.Alignment
shSj = .Range.ParagraphFormat.FirstLineIndent
'lineup1 = .Style.ParagraphFormat.LineUnitBefore
'lineup2 = .Style.ParagraphFormat.SpaceBefore
'linedo1 = .Style.ParagraphFormat.LineUnitAfter
'linedo2 = .Style.ParagraphFormat.SpaceAfter
'fnt = .Range.Font
'pfmt = .Range.ParagraphFormat.LineSpacing
lineup1 = .Range.ParagraphFormat.LineUnitBefore
lineup2 = .Range.ParagraphFormat.SpaceBefore
linedo = .Range.ParagraphFormat.LineUnitAfter
linedo2 = .Range.ParagraphFormat.SpaceAfter
lnSpcrule = .Range.ParagraphFormat.LineSpacingRule
lineSpace = .Range.ParagraphFormat.LineSpacing
.Style = ActiveDocument.Styles("正⽂")
.Range.ListFormat.RemoveNumbers '清除⾃动编号的代码            .Range.Font = fnt
.Range.Font.Name = fntName
.Range.ParagraphFormat.Alignment = alnMent
.Range.Font.Size = fntsize
'.Range.ParagraphFormat.LineSpacing = pfmt
If lineSpace < =1 Then
lineSpace = 1
End If
.Range.ParagraphFormat.LineSpacing = lineSpace
'.Range.ParagraphFormat = pfmt
.Range.ParagraphFormat.FirstLineIndent = shSj
.Range.ParagraphFormat.LineUnitBefore = lineup1
.Range.ParagraphFormat.SpaceBefore = lineup2
.Range.ParagraphFormat.LineUnitAfter = linedo1
.Range.ParagraphFormat.SpaceAfter = linedo2
.Range.ParagraphFormat.LineSpacingRule = lnSpcrule              .Range.ParagraphFormat.LineSpacing = lineSpace
'End If
End With
Next
'设置页⾯⼤⼩
'With ActiveDocument.PageSetup
'        .LineNumbering.Active = False
'        .Orientation = wdOrientPortrait
'        .TopMargin = CentimetersToPoints(3.7)
'        .BottomMargin = CentimetersToPoints(3.5)
'        .LeftMargin = CentimetersToPoints(2.6)
'        .RightMargin = CentimetersToPoints(2.6)
'End With
With ActiveDocument.Styles("标题 1").Font
.NameFarEast = "⽅正⼩标宋简体"
.Size = 22
End With
With ActiveDocument.Styles("标题 1").ParagraphFormat
.LeftIndent = CentimetersToPoints(0)
.RightIndent = CentimetersToPoints(0)
.SpaceBefore = 0
.SpaceBeforeAuto = False
.SpaceAfter = 0
.
SpaceAfterAuto = False
.LineSpacingRule = wdLineSpaceSingle
.Alignment = wdAlignParagraphCenter
.WidowControl = True
.KeepWithNext = False

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。