利用win32com这个api,把文字字符串插入word文档中的一个段落。
为了方便管理,这里是新建了一个类。然后只要实例化这个类,用这个类的方法,传入字符串数据就可以进行使用了。
这个类是DocHandle,要传入word文档的路径(必须是已经存在的),和一个备份的txt文档的路径,防止写入数据的时候word出错关闭什么的。
使用示例:
wordpath=f"./word文档/筛选关键字的.docx"
txtfile = f"./word文档/备份粘贴的文本数据.txt"
#实例化操作word的对象
clsdo=DocHandle(wordpath,txtfile)
#这个是插入字符串段落的方法。
clsdo.gloco("你好,世界")
使用后的示例。
这里上总的代码
import time
import win32com
import os
import pythoncom
from win32com.client import Dispatch
class DocHandle():
# 初始化的数据
def __init__(self,docxfile,txtfile):
# docx文档的路径
self.docxfile=docxfile
#这个是word文档的对象,相关的操作都可以在这里进行。
self.myDoc = None
# 这个是用来储存上一次一样的,防止重复赋值出现。
self.cuncl = ""
# 这个表示前面用过了,然后之后的就不再执行了。
self.tesjx = 0
# 这个是复制多少次,然后保存的。
self.jsbcnm = 0
# 这个是记录初次打开的,要格式页面word之类的。
self.testt = 1
#这是储存在txt文档中的,这是给备份,写入word文档中的,都会在这里收集。
self.txtfile=txtfile
# 检查word中的文本段落数的
def dete_parg_num(self):
tc = self.myDoc.Paragraphs.Count
# 如果是只有1行的话,就给它增加几个段落
if tc==1:
self.myDoc.Paragraphs(int(self.myDoc.Paragraphs.Count)).Range.Paragraphs.Add()
self.myDoc.Paragraphs(int(self.myDoc.Paragraphs.Count)).Range.Paragraphs.Add()
self.myDoc.Paragraphs(int(self.myDoc.Paragraphs.Count)).Range.Paragraphs.Add()
# 打开docx文档
def open_docx(self):
# 如果对象不存在,就新打开一个
if self.myDoc is None:
wordApp = win32com.client.Dispatch('Word.Application')
wordApp.Visible = 1
wordApp.DisplayAlerts = 0
self.myDoc = wordApp.Documents.Open(os.path.abspath(self.docxfile))
# 第一次打开,下面就是按照自己的想要的word做些格式化的操作。
def get_docx_second_format(self,title=None):
self.open_docx()
# 这是初次打开的记录数字
if self.testt == 1:
# 第一次打开,下面就是按照自己的想要的word做些格式化的操作。
self.myDoc.Paragraphs(int(self.myDoc.Paragraphs.Count)).Range.Paragraphs.Add()
self.myDoc.Paragraphs(int(self.myDoc.Paragraphs.Count)).Range.Paragraphs.Add()
self.myDoc.Paragraphs(int(self.myDoc.Paragraphs.Count)).Range.Paragraphs.Add()
tc = self.myDoc.Paragraphs.Count
tzs = int(tc) - int(1)
myRangef = self.myDoc.Paragraphs(int(tzs)).Range
# 1厘米约=28.35磅
myRangef.PageSetup.TopMargin = 5.67 #上边距
myRangef.PageSetup.BottomMargin = 5.67 #下边距
myRangef.PageSetup.LeftMargin = 14.175 #左边距
myRangef.PageSetup.RightMargin = 14.175 #右边距
myRangef.PageSetup.PageWidth = 425.25 #页面宽度
self.myDoc.Paragraphs(int(self.myDoc.Paragraphs.Count)).Range.Paragraphs.Add()
tc = self.myDoc.Paragraphs.Count
tzs = int(tc) - int(1)
myRangef = self.myDoc.Paragraphs(int(tzs)).Range
# 给初始打开加入一个时间
tastime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
# title是传入这个方法的标题。
if title is not None:
# 写入word中。
myRangef.Text = f"{tastime}/{title}>>"
fo = open(self.txtfile, "a+", encoding='utf-8')
fo.write(f"\n{tastime}/{title}>>\n")
fo.close()
else:
myRangef.Text = f"{tastime}"
fo = open(self.txtfile, "a+", encoding='utf-8')
fo.write(f"\n{tastime}\n")
fo.close()
self.myDoc.Paragraphs(int(self.myDoc.Paragraphs.Count)).Range.Paragraphs.Add()
self.myDoc.Paragraphs(int(self.myDoc.Paragraphs.Count)).Range.Paragraphs.Add()
#这个数字表示,文档已经不是初次打开的,就不执行那些初始化的操作。
self.testt = 2
#这是添加标题的,分行。
def zj_title_format(self,data):
#这个方法是测试是否只有一个段落,然后做些处理,不然会有问题。
self.dete_parg_num()
# 这是当文本有,第,章,卷的时候,分行,并且添加一个时间。
if (str(data).find("第") != -1 and (str(data).find("章") != -1 or str(data).find("卷") != -1) and
data != self.cuncl and self.tesjx != 1):
self.cuncl = data
if (self.cuncl.find("\n") != -1):
self.cuncl = self.cuncl.replace("\n", "").replace('\r', 'ks')
self.cuncl = self.cuncl.replace("ksks", "\n")
self.cuncl = self.cuncl.replace("ks", "\n")
self.myDoc.Paragraphs(int(self.myDoc.Paragraphs.Count)).Range.Paragraphs.Add()
tc = self.myDoc.Paragraphs.Count
tzs = int(tc) - int(1)
myRangef = self.myDoc.Paragraphs(int(tzs)).Range
tastime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
myRangef.Text = self.cuncl + ' \\' + tastime
jjlpo=self.cuncl + ' \\' + tastime
fo = open(self.txtfile, "a+", encoding='utf-8')
fo.write(f"{jjlpo}\n")
fo.close()
self.myDoc.Paragraphs(int(self.myDoc.Paragraphs.Count)).Range.Paragraphs.Add()
self.myDoc.Paragraphs(int(self.myDoc.Paragraphs.Count)).Range.Paragraphs.Add()
self.tesjx = 1
self.cuncl = data
#这是添加一般的文本的。
def copy_word_format(self,data):
self.dete_parg_num()
#这个数据,是用来辨别文本是否已经被标题写入的,如果已经被写入了,就不再写入了。
if (self.tesjx != 1):
if (data != self.cuncl):
self.cuncl = data
if (self.cuncl.find("\n") != -1):
self.cuncl = "\n" + self.cuncl
tc = self.myDoc.Paragraphs.Count
tzs = int(tc) - int(1)
myRangef = self.myDoc.Paragraphs(int(tzs)).Range
myRangef.Text = self.cuncl
fo = open(self.txtfile, "a+", encoding='utf-8')
fo.write(f"{self.cuncl}\n")
fo.close()
self.myDoc.Paragraphs(int(self.myDoc.Paragraphs.Count)).Range.Paragraphs.Add()
self.myDoc.Paragraphs(int(self.myDoc.Paragraphs.Count)).Range.Paragraphs.Add()
self.tesjx = 1
self.cuncl = data
#就是要把数字放在第一个位置,才有参数。
def gloco(self,aass,title=None):
#这是初始化一个win32com的代码,要有这个,不然会有异常。
pythoncom.CoInitialize()
#第一次打开,格式化操作。
self.get_docx_second_format(title)
#你要写入的文本,是aass
data = aass
# 下面是对文本的写入操作
self.zj_title_format(data)
self.copy_word_format(data)
# 这是计算写入的次数
self.jsbcnm = self.jsbcnm + 1
#这是每写一个,就进行保存。
if (self.jsbcnm == 1):
self.myDoc.SaveAs(self.docxfile)
self.jsbcnm = 0
#这时候文本已经被写入了,所以要初始化为0,让之后也可以写。
if (self.tesjx == 1):
self.tesjx = 0
if __name__ == '__main__':
wordpath=""
txtfile =""
clsdo=DocHandle(wordpath,txtfile)
clsdo.gloco("你好,世界")
pass
如何做自己的word文档的操作?
DocHandle中有一个属性是self.myDoc,这就是那个文档的对象,相关的文档操作都是从这里进行的。
如果想写一些自动化操作的功能,可以自己录制宏,然后参考宏编辑器的代码
如果想要自己添加其他的对word的操作,可以参考官方的api