根据“雷石安全实验室”《一周网安动态》自动生成安全通告
声明获取内容来自“雷石安全实验室”公众号。
根据雷石安全实验室一周网安动态自动生成安全通告
date 获取日期
import requests
import re
def date():
f = open('1.txt', 'r')
a = f.read()
print(a)
f.close()
resp=requests.get(a)
resp=resp.text
pattern= re.compile(r'\d\d\d\d\d\d\d\d-\d\d\d\d\d\d\d\d') ###一次正则取日期20220110-20220117
date = pattern.findall(resp)[0] ###一次正则匹配结果20220110-20220117
#chapters_url_1 = ''.join(chapters_url_1) ####列表转字符串
pattern1=re.compile(r'-\d\d\d\d\d\d\d\d') # 二次正则取最终日期
date_end=pattern1.findall(date)[0]
date_end=date_end.replace('-','')
print(date_end)
return a,date,date_end
# print(date()[2])
web.py 爬取网页内容,并return
import os
from typing import List
from selenium import webdriver
from date import date
import os
import xlrd
from xlutils.copy import copy
from openpyxl import workbook
from xlwt import Workbook
import re
#url="https://mp.weixin.qq.com/s/1_TAePRauIRxihEZ8hIxeg"
path=date()[0]
def test(path) -> List:
opt = webdriver.ChromeOptions()
#opt.set_headless()
driver_dir = os.path.dirname(os.path.abspath(__file__))
driver = webdriver.Chrome(options=opt, executable_path=r".\chromedriver.exe")
driver.get(path)
# 内容预览
Neirong_yulan = driver.find_elements_by_xpath('//*[@id="js_content"]/section/section['
'1]/section/section/section/section/section/section['
'4]/section/p/strong')[0].text
Neirong_yulan = Neirong_yulan.replace('#', '')
Neirong_yulan='1. '+Neirong_yulan
print(Neirong_yulan)
# Part1
Part1 = driver.find_element_by_xpath('//*[@id="js_content"]/section/section['
'1]/section/section/section/section/section/section[6]/p/strong').text
Part1='1.1 '+Part1
print(Part1)
# part1列表
Part1_1 = driver.find_element_by_xpath('//*[@id="js_content"]/section/section['
'1]/section/section/section/section/section/section['
'7]/section/section/section/section/p').text
Part1_1 = Part1_1.replace(' ','')
print(Part1_1)
Part1_2 = driver.find_element_by_xpath('//*[@id="js_content"]/section/section[1]/section/section/section/section/section/section[8]/section/section/section/section/p').text
Part1_2 = Part1_2.replace(' ','')
print(Part1_2)
Part1_3 = driver.find_element_by_xpath(
'//*[@id="js_content"]/section/section[1]/section/section/section/section/section/section[9]/section/section/section/section/p').text
Part1_3 = Part1_3.replace(' ', '')
print(Part1_3)
Part1_4 = driver.find_element_by_xpath(
'//*[@id="js_content"]/section/section[1]/section/section/section/section/section/section[10]/section/section/section/section/p').text
Part1_4 = Part1_4.replace(' ', '')
print(Part1_4)
# part2列表
Part2 = driver.find_element_by_xpath('//*[@id="js_content"]/section/section[1]/section/section/section/section/section/section[11]/p/strong').text
Part2 = '1.2 ' + Part2
print(Part2)
Part2_1 = driver.find_element_by_xpath('//*[@id="js_content"]/section/section[1]/section/section/section/section/section/section[12]/section/section/section/section/p').text
Part2_1 = Part2_1.replace(' ','')
print(Part2_1)
Part2_2 = driver.find_element_by_xpath('//*[@id="js_content"]/section/section[1]/section/section/section/section/section/section[13]/section/section/section/section/p').text
Part2_2 = Part2_2.replace(' ','')
print(Part2_2)
Part2_3 = driver.find_element_by_xpath(
'//*[@id="js_content"]/section/section[1]/section/section/section/section/section/section[14]/section/section/section/section/p').text
Part2_3 = Part2_3.replace(' ', '')
print(Part2_3)
Part2_4 = driver.find_element_by_xpath(
'//*[@id="js_content"]/section/section[1]/section/section/section/section/section/section[15]/section/section/section/section/p').text
Part2_4 = Part2_4.replace(' ', '')
print(Part2_4)
# Part3
Part3 = driver.find_element_by_xpath('//*[@id="js_content"]/section/section[1]/section/section/section/section/section/section[16]/p/span/strong').text
Part3 = '1.3 ' + Part3
print(Part3)
Part3_1 = driver.find_element_by_xpath('//*[@id="js_content"]/section/section[1]/section/section/section/section/section/section[17]/section/section/section/section/p').text
Part3_1 = Part3_1.replace(' ','')
print(Part3_1)
Part3_2 = driver.find_elements_by_xpath('//*[@id="js_content"]/section/section[1]/section/section/section/section/section/section[18]/section/section/section/section/p')[0].text
Part3_2 = Part3_2.replace(' ','')
print(Part3_2)
Part3_3 = driver.find_elements_by_xpath(
'//*[@id="js_content"]/section/section[1]/section/section/section/section/section/section[18]/section/section/section/section/p')[1].text
Part3_3 = Part3_3.replace(' ', '')
print(Part3_3)
Part3_4 = driver.find_element_by_xpath(
'//*[@id="js_content"]/section/section[1]/section/section/section/section/section/section[19]/section/section/section/section/p').text
Part3_4 = Part3_4.replace(' ', '')
print(Part3_4)
# 国内资讯 #
Guonei_zixun = driver.find_elements_by_xpath('//*[@id="js_content"]/section/section/section/section/section/section/section/section[21]/section[1]/p/strong')[0].text
Guonei_zixun = Guonei_zixun.replace('#', '')
Guonei_zixun = '2. ' + Guonei_zixun
print(Guonei_zixun)
'''01xxx'''
Guonei_zixun_1 = driver.find_element_by_xpath('//*[@id="js_content"]/section/section[1]/section/section/section/section/section/section[21]/section[4]/section[1]/section[2]/section/section[2]').text
Guonei_zixun_1 = Guonei_zixun_1.replace('\n','')
Guonei_zixun_1 = '2.1 ' +Guonei_zixun_1
print(Guonei_zixun_1)
Guonei_zixun_1_text=driver.find_element_by_xpath('//*[@id="js_content"]/section/section[1]/section/section/section/section/section/section[21]/section[4]/section[2]/section/section[1]').text
print(Guonei_zixun_1_text)
'''02xxx'''
Guonei_zixun_2 = driver.find_element_by_xpath('//*[@id="js_content"]/section/section[1]/section/section/section/section/section/section[22]/section[1]/section/section[1]/section[2]/section/section[2]').text
Guonei_zixun_2 = Guonei_zixun_2.replace('\n','')
Guonei_zixun_2 = '2.2 ' + Guonei_zixun_2
print(Guonei_zixun_2)
Guonei_zixun_2_text=driver.find_element_by_xpath('//*[@id="js_content"]/section/section[1]/section/section/section/section/section/section[22]/section[1]/section/section[2]/section/section[1]').text
print(Guonei_zixun_2_text)
'''03xxx'''
Guonei_zixun_3 = driver.find_element_by_xpath(
'//*[@id="js_content"]/section/section[1]/section/section/section/section/section/section[22]/section[3]/section[1]/section[1]/section[2]/section[1]/section[2]/section[2]').text
Guonei_zixun_3 = Guonei_zixun_3.replace('\n', '')
Guonei_zixun_3 = '2.3 ' + Guonei_zixun_3
print(Guonei_zixun_3)
Guonei_zixun_3_text=driver.find_element_by_xpath('//*[@id="js_content"]/section/section[1]/section/section/section/section/section/section[22]/section[3]/section[1]/section[2]/section/section/section').text
print(Guonei_zixun_3_text)
'''04xxx'''
Guonei_zixun_4 = driver.find_element_by_xpath(
'//*[@id="js_content"]/section/section[1]/section/section/section/section/section/section[22]/section[3]/section[3]/section/section[1]/section[2]/section[1]/section[2]/section[2]').text
Guonei_zixun_4 = Guonei_zixun_4.replace('\n', '')
print(Guonei_zixun_4)
Guonei_zixun_4 = '2.4 '+ Guonei_zixun_4
try:
Guonei_zixun_4_text=driver.find_element_by_xpath('//*[@id="js_content"]/section/section[1]/section/section/section/section/section/section[22]/section[3]/section[3]/section/section[2]/section/section').text
except:
Guonei_zixun_4_text = driver.find_element_by_xpath('//*[@id="js_content"]/section/section/section/section/section/section/section/section[22]/section[3]/section[3]/section/section[3]/section/section/section[1]/p').text
print(Guonei_zixun_4_text)
# 国外资讯 #
Guowai_zixun=driver.find_element_by_xpath('//*[@id="js_content"]/section/section[1]/section/section/section/section/section/section[22]/section[3]/section[4]/section/p/strong').text
Guowai_zixun = Guowai_zixun.replace('#', '')
Guowai_zixun = '3. ' + Guowai_zixun
print(Guowai_zixun)
'''01xxx'''
Guowai_zixun_1 = driver.find_element_by_xpath(
'//*[@id="js_content"]/section/section[1]/section/section/section/section/section/section[22]/section[3]/section[8]/section/section[1]/section[2]/section/section[2]/section[2]').text
Guowai_zixun_1 = Guowai_zixun_1.replace('\n', '')
Guowai_zixun_1 = '3.1 ' + Guowai_zixun_1
print(Guowai_zixun_1)
Guowai_zixun_1_text=driver.find_element_by_xpath('//*[@id="js_content"]/section/section[1]/section/section/section/section/section/section[22]/section[3]/section[8]/section/section[2]/section[1]/section[1]/section').text
print(Guowai_zixun_1_text)
'''02xxx'''
Guowai_zixun_2 = driver.find_element_by_xpath(
'//*[@id="js_content"]/section/section[1]/section/section/section/section/section/section[22]/section[3]/section[9]/section[2]/section[1]/section[2]/section[2]').text
Guowai_zixun_2 = Guowai_zixun_2.replace('\n', '')
Guowai_zixun_2 = '3.2 ' + Guowai_zixun_2
print(Guowai_zixun_2)
Guowai_zixun_2_text=driver.find_element_by_xpath('//*[@id="js_content"]/section/section[1]/section/section/section/section/section/section[22]/section[3]/section[10]/section/section/section[1]/section').text
print(Guowai_zixun_2_text)
'''03xxx'''
Guowai_zixun_3 = driver.find_element_by_xpath(
'//*[@id="js_content"]/section/section[1]/section/section/section/section/section/section[22]/section[3]/section[12]/section/section[1]/section[2]/section/section[2]/section[2]').text
Guowai_zixun_3 = Guowai_zixun_3.replace('\n', '')
Guowai_zixun_3 = '3.3 ' + Guowai_zixun_3
print(Guowai_zixun_3)
try:
Guowai_zixun_3_text = driver.find_element_by_xpath(
'//*[@id="js_content"]/section/section[1]/section/section/section/section/section/section[22]/section[3]/section[12]/section/section[2]/section/section[1]/section').text
except:
Guowai_zixun_3_text = driver.find_element_by_xpath(
'//*[@id="js_content"]/section/section/section/section/section/section/section/section[22]/section[3]/section[12]/section/section[3]/section/section[1]/section').text
print(Guowai_zixun_3_text)
'''04xxx'''
Guowai_zixun_4 = driver.find_element_by_xpath(
'//*[@id="js_content"]/section/section[1]/section/section/section/section/section/section[22]/section[3]/section[14]/section/section[1]/section[2]/section/section[2]/section[2]').text
Guowai_zixun_4 = Guowai_zixun_4.replace('\n', '')
Guowai_zixun_4 = '3.4 ' + Guowai_zixun_4
print(Guowai_zixun_4)
Guowai_zixun_4_text=driver.find_element_by_xpath('//*[@id="js_content"]/section/section[1]/section/section/section/section/section/section[22]/section[3]/section[14]/section/section[2]/section/section[1]/section').text
print(Guowai_zixun_4_text)
# 威胁情报 #
weixieqingbao=driver.find_element_by_xpath('//*[@id="js_content"]/section/section[1]/section/section/section/section/section/section[22]/section[3]/section[15]/section/p/strong').text
weixieqingbao = weixieqingbao.replace('#', '')
weixieqingbao = '4. '+ weixieqingbao
print(weixieqingbao)
'''0X001'''
Weixie_qingbao_1 = driver.find_element_by_xpath(
'//*[@id="js_content"]/section/section[1]/section/section/section/section/section/section[22]/section[3]/section[19]/section/section[1]/section[2]/section/section[2]/section[2]').text
Weixie_qingbao_1 = Weixie_qingbao_1.replace('\n', '')
Weixie_qingbao_1 = '4.1 ' + Weixie_qingbao_1
print(Weixie_qingbao_1)
Weixie_qingbao_1_text=driver.find_element_by_xpath('//*[@id="js_content"]/section/section[1]/section/section/section/section/section/section[22]/section[3]/section[19]/section/section[2]/section/section[1]/section').text
print(Weixie_qingbao_1_text)
'''0X002'''
Weixie_qingbao_2 = driver.find_element_by_xpath(
'//*[@id="js_content"]/section/section[1]/section/section/section/section/section/section[22]/section[3]/section[21]/section/section[1]/section[2]/section/section[2]/section[2]').text
Weixie_qingbao_2 = Weixie_qingbao_2.replace('\n', '')
Weixie_qingbao_2 = '4.2 ' + Weixie_qingbao_2
print(Weixie_qingbao_2)
try:
Weixie_qingbao_2_text=driver.find_element_by_xpath('//*[@id="js_content"]/section/section[1]/section/section/section/section/section/section[22]/section[3]/section[21]/section/section[2]/section/section[1]/section').text
except:
Weixie_qingbao_2_text=driver.find_element_by_xpath('//*[@id="js_content"]/section/section[1]/section/section/section/section/section/section[22]/section[3]/section[21]/section/section[3]/section/section[1]/section').text
print(Weixie_qingbao_2_text)
'''0X003'''
Weixie_qingbao_3 = driver.find_element_by_xpath(
'//*[@id="js_content"]/section/section[1]/section/section/section/section/section/section[22]/section[3]/section[23]/section/section[1]/section[2]/section/section[2]/section[2]').text
Weixie_qingbao_3 = Weixie_qingbao_3.replace('\n', '')
Weixie_qingbao_3 = '4.3 ' + Weixie_qingbao_3
print(Weixie_qingbao_3)
Weixie_qingbao_3_text=driver.find_element_by_xpath('//*[@id="js_content"]/section/section[1]/section/section/section/section/section/section[22]/section[3]/section[23]/section/section[2]/section/section[1]/section').text
print(Weixie_qingbao_3_text)
'''0X004'''
Weixie_qingbao_4 = driver.find_element_by_xpath(
'//*[@id="js_content"]/section/section[1]/section/section/section/section/section/section[22]/section[3]/section[25]/section/section[1]/section[2]/section/section[2]/section[2]/p/strong').text
Weixie_qingbao_4 = Weixie_qingbao_4.replace('\n', '')
Weixie_qingbao_4 = '4.4 ' + Weixie_qingbao_4
print(Weixie_qingbao_4)
Weixie_qingbao_4_text=driver.find_element_by_xpath('//*[@id="js_content"]/section/section[1]/section/section/section/section/section/section[22]/section[3]/section[25]/section/section[2]/section/section[1]/section').text
print(Weixie_qingbao_4_text)
# Guowai_zixun_2 = driver.find_element_by_xpath(
# '').text
# Guowai_zixun_2 = Guowai_zixun_2.replace('\n', '')
# print(Guowai_zixun_2)
# Guowai_zixun_1_text=driver.find_element_by_xpath('').text
# print(Guowai_zixun_1_text)
driver.close()
return [Neirong_yulan,
Part1, Part1_1, Part1_2, Part1_3, Part1_4,
Part2, Part2_1, Part2_2, Part2_3, Part2_4,
Part3, Part3_1,Part3_2, Part3_3, Part3_4,
Guonei_zixun,Guonei_zixun_1,Guonei_zixun_1_text,Guonei_zixun_2, Guonei_zixun_2_text,
Guonei_zixun_3, Guonei_zixun_3_text, Guonei_zixun_4,Guonei_zixun_4_text,
Guowai_zixun,Guowai_zixun_1, Guowai_zixun_1_text,Guowai_zixun_2, Guowai_zixun_2_text,Guowai_zixun_3, Guowai_zixun_3_text,
Guowai_zixun_4,Guowai_zixun_4_text,
weixieqingbao,Weixie_qingbao_1,Weixie_qingbao_1_text,Weixie_qingbao_2,
Weixie_qingbao_2_text,Weixie_qingbao_3,Weixie_qingbao_3_text,Weixie_qingbao_4,Weixie_qingbao_4_text]
a=test(path)
# x=0
# for i in a:
# print(x,i)
# x=x+1
# -*- coding: utf-8 -*-
"""
Created on Sat Oct 24 17:21:31 2020
pip install -i https://mirrors.aliyun.com/pypi/simple/ docx
参考文档 https://python-docx.readthedocs.io/en/latest/
@author: Administrator
"""
from docx import Document
from docx.shared import Inches
from docx.oxml import OxmlElement
from docx.oxml.ns import qn
#设置表格的边框
def set_cell_border(cell, **kwargs):
"""
Set cell`s border
Usage:
set_cell_border(
cell,
top={"sz": 12, "val": "single", "color": "#FF0000", "space": "0"},
bottom={"sz": 12, "color": "#00FF00", "val": "single"},
left={"sz": 24, "val": "dashed", "shadow": "true"},
right={"sz": 12, "val": "dashed"},
)
"""
tc = cell._tc
tcPr = tc.get_or_add_tcPr()
# check for tag existnace, if none found, then create one
tcBorders = tcPr.first_child_found_in("w:tcBorders")
if tcBorders is None:
tcBorders = OxmlElement('w:tcBorders')
tcPr.append(tcBorders)
# list over all available tags
for edge in ('left', 'top', 'right', 'bottom', 'insideH', 'insideV'):
edge_data = kwargs.get(edge)
if edge_data:
tag = 'w:{}'.format(edge)
# check for tag existnace, if none found, then create one
element = tcBorders.find(qn(tag))
if element is None:
element = OxmlElement(tag)
tcBorders.append(element)
# looks like order of attributes is important
for key in ["sz", "val", "color", "space", "shadow"]:
if key in edge_data:
element.set(qn('w:{}'.format(key)), str(edge_data[key]))
document = Document()
#document.add_picture('monty-truth.png', width=Inches(1.25))
records = (
(3, '101', 'Spam'),
(7, '422', 'Eggs'),
(4, '631', 'Spam, spam, eggs, and spam')
)
table = document.add_table(rows=1, cols=1)
hdr_cells = table.rows[0].cells
hdr_cells[0].text ="xxx"
set_cell_border(
hdr_cells[0],
top={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"},
bottom={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"},
left={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"},
right={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"},
insideH={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"},
end={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"}
)
# hdr_cells[1].text = 'Id'
# hdr_cells[2].text = 'Desc'
# for qty, id, desc in records:
# row_cells = table.add_row().cells
# row_cells[0].text = str(qty)
# row_cells[1].text = id
# row_cells[2].text = desc
print('finish !!!')
tables设置表格样式,写入表格
# -*- coding: utf-8 -*-
"""
Created on Sat Oct 24 17:21:31 2020
pip install -i https://mirrors.aliyun.com/pypi/simple/ docx
参考文档 https://python-docx.readthedocs.io/en/latest/
@author: Administrator
"""
from docx import Document
from docx.shared import Inches
from docx.oxml import OxmlElement
from docx.oxml.ns import qn
#设置表格的边框
def set_cell_border(cell, **kwargs):
"""
Set cell`s border
Usage:
set_cell_border(
cell,
top={"sz": 12, "val": "single", "color": "#FF0000", "space": "0"},
bottom={"sz": 12, "color": "#00FF00", "val": "single"},
left={"sz": 24, "val": "dashed", "shadow": "true"},
right={"sz": 12, "val": "dashed"},
)
"""
tc = cell._tc
tcPr = tc.get_or_add_tcPr()
# check for tag existnace, if none found, then create one
tcBorders = tcPr.first_child_found_in("w:tcBorders")
if tcBorders is None:
tcBorders = OxmlElement('w:tcBorders')
tcPr.append(tcBorders)
# list over all available tags
for edge in ('left', 'top', 'right', 'bottom', 'insideH', 'insideV'):
edge_data = kwargs.get(edge)
if edge_data:
tag = 'w:{}'.format(edge)
# check for tag existnace, if none found, then create one
element = tcBorders.find(qn(tag))
if element is None:
element = OxmlElement(tag)
tcBorders.append(element)
# looks like order of attributes is important
for key in ["sz", "val", "color", "space", "shadow"]:
if key in edge_data:
element.set(qn('w:{}'.format(key)), str(edge_data[key]))
document = Document()
#document.add_picture('monty-truth.png', width=Inches(1.25))
records = (
(3, '101', 'Spam'),
(7, '422', 'Eggs'),
(4, '631', 'Spam, spam, eggs, and spam')
)
table = document.add_table(rows=1, cols=1)
hdr_cells = table.rows[0].cells
hdr_cells[0].text ="xxx"
set_cell_border(
hdr_cells[0],
top={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"},
bottom={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"},
left={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"},
right={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"},
insideH={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"},
end={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"}
)
# hdr_cells[1].text = 'Id'
# hdr_cells[2].text = 'Desc'
# for qty, id, desc in records:
# row_cells = table.add_row().cells
# row_cells[0].text = str(qty)
# row_cells[1].text = id
# row_cells[2].text = desc
print('finish !!!')
test 写入word
# -*- coding: UTF-8 -*-
# @Project :test
import os
from docx import Document
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
from docx.shared import Inches
from docx.shared import Pt #磅数
from docx.oxml.ns import qn #中文格式
from docx.shared import RGBColor#设置字体
from win32com import client
from web import test
from docx.enum.style import WD_STYLE_TYPE
from tables import set_cell_border
from date import date
import getpass
# date函数取url
path = date()[0]
# 执行web.test获得爬取的内容
rest_text = test(path)
date1='(' + date()[1] +')'
date_end=date()[2]
def start():
document = Document()
# 设置文档的基础字体中文
document.styles['Normal'].font.name = u'Times New Roman'
# 设置文档的基础字体西文
document.styles['Normal'].element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')
# 小四 12磅
document.styles["Normal"].font.size = Pt(12)
# 黑色
document.styles["Normal"].font.color.rgb = RGBColor(0, 0, 0)
# 人太菜添加了5个回车。。。。。。。。
pl_1 = document.add_paragraph()
pl_1.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
pl_2 = document.add_paragraph()
pl_2.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
pl_3 = document.add_paragraph()
pl_3.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
pl_4 = document.add_paragraph()
pl_4.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
pl_5 = document.add_paragraph()
pl_5.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
# 初始化建立第一个自然段
pl = document.add_paragraph()
# 对齐方式为居中,没有这句话默认左对齐
pl.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
run1 = pl.add_run('xxx公司')
run1.font.name = '等线'
run1.element.rPr.rFonts.set(qn('w:eastAsia'), u'等线')
# 一号为26磅
run1.font.size = Pt(26)
# 是否加粗
run1.font.bold = True
# 前后距离5磅
pl.space_before = Pt(5)
pl.space_after = Pt(5)
'''安全通告'''
pl1 = document.add_paragraph()
pl1.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
run2 = pl1.add_run('安全通告')
run2.font.name = '等线'
run2.element.rPr.rFonts.set(qn('w:eastAsia'), u'等线')
run2.font.size = Pt(26)
run2.font.bold = True
# 前后距离5磅
pl1.space_before = Pt(5)
pl1.space_after = Pt(5)
# 是否加粗
run1.font.bold = True
# 第6个回车
pl_6 = document.add_paragraph()
'''首页日期'''
pl3 = document.add_paragraph()
pl3.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
run3 = pl3.add_run(date1)
run3.font.name = '等线'
run3.element.rPr.rFonts.set(qn('w:eastAsia'), u'等线')
# 小四为12磅
run3.font.size = Pt(12)
# 是否加粗
run3.font.bold = True
# 前后距离5磅
pl3.space_before = Pt(5)
pl3.space_after = Pt(5)
'''添加十个回车,,,,太菜 加10个换行'''
pl4 = document.add_paragraph()
run4 = pl3.add_run("\n\n\n\n\n\n\n\n\n\n")
run4.font.name = '等线'
run4.element.rPr.rFonts.set(qn('w:eastAsia'), u'等线')
# 小四为12磅
run3.font.size = Pt(12)
'''通告日期'''
pl5 = document.add_paragraph()
pl5.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
run5 = pl5.add_run(date_end)
run5.font.name = '等线'
run5.element.rPr.rFonts.set(qn('w:eastAsia'), u'等线')
# 小四为12磅
run5.font.size = Pt(12)
run5.font.bold = True
'''单位名称'''
pl6 = document.add_paragraph()
pl6.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
run6 = pl6.add_run("xxx公司")
run6.font.name = '等线'
run6.element.rPr.rFonts.set(qn('w:eastAsia'), u'等线')
# 小四为12磅
run6.font.size = Pt(12)
run6.font.bold = True
# # 插入分页符
# document.add_page_break()
''' 分节符——下一节'''
document.add_section()
pl4_1 = document.add_paragraph()
pl4_1 = document.add_paragraph()
#
# #添加段落,文本可以包含制表符(\t)、换行符(\n)或回车符(\r)等
# p = document.add_paragraph('\r\r\r^p\r\r')
# p.add_run("xxx公司\r安全通告\r").bold = True
styles = document.styles
# for s in styles:
# if s.type == WD_STYLE_TYPE.PARAGRAPH:
# document.add_paragraph('Paragraph style is : '+ s.name, style = s)
# 选取 style,并设置 style 中的段落格式
styles = document.styles
# document.styles['Normal'].font.name = u'宋体'
# # 设置文档的基础字体西文
# document.styles['Normal'].element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')
# # 小四 12磅
# document.styles["Normal"].font.size=Pt(12)
# 选取 style,并设置 style 中的段落格式
style = styles['Heading 1']
style.font.name = u'宋体'
para_format = style.paragraph_format
# 二号对应22磅
para_format.left_indent = Pt(22)
para_format.widow_control = True
style2 = styles['Heading 2']
para_format2 = style2.paragraph_format
# 小三对应15磅
para_format2.left_indent = Pt(15)
para_format2.widow_control = True
# 将设置好段落格式的 style 运用到段落中
# T1 = document.add_paragraph(rest_text[0], style=style)
T1_1 = document.add_heading('', level=1)
run_T1_1 = T1_1.add_run(rest_text[0])
run_T1_1.font.name = 'Times New Roman'
run_T1_1.element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')
run_T1_1.font.color.rgb = RGBColor(0, 0, 0)
run_T1_1.font.size = Pt(22)
run_T1_1.font.bold = True
run_T1_1.alignment = WD_PARAGRAPH_ALIGNMENT.LEFT
# T2 = document.add_paragraph(rest_text[1], style=style2)
T2 = document.add_heading('', level=2)
run_T2 = T2.add_run(rest_text[1])
run_T2.font.name = 'Times New Roman'
run_T2.element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')
run_T2.font.color.rgb = RGBColor(0, 0, 0)
# 小三
run_T2.font.size = Pt(15)
run_T2.font.bold = True
T3 = document.add_paragraph(rest_text[2])
T4 = document.add_paragraph(rest_text[3])
T5 = document.add_paragraph(rest_text[4])
T5 = document.add_paragraph(rest_text[5])
# T6 = document.add_paragraph(rest_text[6], style=style2)
T6 = document.add_heading('', level=2)
run_T6 = T6.add_run(rest_text[6])
run_T6.font.name = 'Times New Roman'
run_T6.element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')
run_T6.font.color.rgb = RGBColor(0, 0, 0)
# 小三
run_T6.font.size = Pt(15)
run_T6.font.bold = True
T7 = document.add_paragraph(rest_text[7])
T8 = document.add_paragraph(rest_text[8])
T9 = document.add_paragraph(rest_text[9])
T10 = document.add_paragraph(rest_text[10])
# T11 = document.add_paragraph(rest_text[11], style=style2)
T11 = document.add_heading('', level=2)
run_11 = T11.add_run(rest_text[11])
run_11.font.name = 'Times New Roman'
run_11.element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')
run_11.font.color.rgb = RGBColor(0, 0, 0)
# 小三
run_11.font.size = Pt(15)
run_11.font.bold = True
T12 = document.add_paragraph(rest_text[12])
T13 = document.add_paragraph(rest_text[13])
T14 = document.add_paragraph(rest_text[14])
T15 = document.add_paragraph(rest_text[15])
# T16 = document.add_paragraph(rest_text[16], style=style)
T16 = document.add_heading('', level=1)
run_T16 = T16.add_run(rest_text[16])
run_T16.font.name = 'Times New Roman'
run_T16.element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')
run_T16.font.color.rgb = RGBColor(0, 0, 0)
run_T16.font.size = Pt(22)
run_T16.font.bold = True
# T17 = document.add_paragraph(rest_text[17], style=style2)
T17 = document.add_heading('', level=2)
run_17 = T17.add_run(rest_text[17])
run_17.font.name = 'Times New Roman'
run_17.element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')
run_17.font.color.rgb = RGBColor(0, 0, 0)
# 小三
run_17.font.size = Pt(15)
run_17.font.bold = True
table1 = document.add_table(rows=1, cols=1)
hdr_cells = table1.rows[0].cells
hdr_cells[0].text = rest_text[18]
set_cell_border(
hdr_cells[0],
top={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"},
bottom={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"},
left={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"},
right={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"},
insideH={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"},
end={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"}
)
# T19 = document.add_paragraph(rest_text[19], style=style2)
T19 = document.add_heading('', level=2)
run_19 = T19.add_run(rest_text[19])
run_19.font.name = 'Times New Roman'
run_19.element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')
run_19.font.color.rgb = RGBColor(0, 0, 0)
# 小三
run_19.font.size = Pt(15)
run_19.font.bold = True
table2 = document.add_table(rows=1, cols=1)
hdr_cells2 = table2.rows[0].cells
hdr_cells2[0].text = rest_text[20]
set_cell_border(
hdr_cells2[0],
top={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"},
bottom={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"},
left={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"},
right={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"},
insideH={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"},
end={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"}
)
# T21 = document.add_paragraph(rest_text[21], style=style2)
T21 = document.add_heading('', level=2)
run_21 = T21.add_run(rest_text[21])
run_21.font.name = 'Times New Roman'
run_21.element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')
run_21.font.color.rgb = RGBColor(0, 0, 0)
# 小三
run_21.font.size = Pt(15)
run_21.font.bold = True
table3 = document.add_table(rows=1, cols=1)
hdr_cells3 = table3.rows[0].cells
hdr_cells3[0].text = rest_text[22]
set_cell_border(
hdr_cells3[0],
top={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"},
bottom={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"},
left={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"},
right={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"},
insideH={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"},
end={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"}
)
# T23 = document.add_paragraph(rest_text[23], style=style2)
T23 = document.add_heading('', level=2)
run_23 = T23.add_run(rest_text[23])
run_23.font.name = 'Times New Roman'
run_23.element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')
run_23.font.color.rgb = RGBColor(0, 0, 0)
# 小三
run_23.font.size = Pt(15)
run_23.font.bold = True
table4 = document.add_table(rows=1, cols=1)
hdr_cells4 = table4.rows[0].cells
hdr_cells4[0].text = rest_text[24]
set_cell_border(
hdr_cells4[0],
top={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"},
bottom={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"},
left={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"},
right={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"},
insideH={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"},
end={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"}
)
# T25 = document.add_paragraph(rest_text[25], style=style)
T25 = document.add_heading('', level=1)
run_25 = T25.add_run(rest_text[25])
run_25.font.name = "Times New Roman"
run_25.element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')
run_25.font.color.rgb = RGBColor(0, 0, 0)
# 小三
run_25.font.size = Pt(22)
run_25.font.bold = True
# T26 = document.add_paragraph(rest_text[26], style=style2)
T26 = document.add_heading('', level=2)
run_26 = T26.add_run(rest_text[26])
run_26.font.name = 'Times New Roman'
run_26.element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')
run_26.font.color.rgb = RGBColor(0, 0, 0)
# 小三
run_26.font.size = Pt(15)
run_26.font.bold = True
table5 = document.add_table(rows=1, cols=1)
hdr_cells5 = table5.rows[0].cells
hdr_cells5[0].text = rest_text[27]
set_cell_border(
hdr_cells5[0],
top={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"},
bottom={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"},
left={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"},
right={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"},
insideH={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"},
end={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"}
)
# T28 = document.add_paragraph(rest_text[28], style=style2)
T28 = document.add_heading('', level=2)
run_28 = T28.add_run(rest_text[28])
run_28.font.name = 'Times New Roman'
run_28.element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')
run_28.font.color.rgb = RGBColor(0, 0, 0)
# 小三
run_28.font.size = Pt(15)
run_28.font.bold = True
table6 = document.add_table(rows=1, cols=1)
hdr_cells6 = table6.rows[0].cells
hdr_cells6[0].text = rest_text[29]
set_cell_border(
hdr_cells6[0],
top={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"},
bottom={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"},
left={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"},
right={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"},
insideH={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"},
end={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"}
)
# T30 = document.add_paragraph(rest_text[30], style=style2)
T30 = document.add_heading('', level=2)
run_30 = T30.add_run(rest_text[30])
run_30.font.name = 'Times New Roman'
run_30.element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')
run_30.font.color.rgb = RGBColor(0, 0, 0)
# 小三
run_30.font.size = Pt(15)
run_30.font.bold = True
table7 = document.add_table(rows=1, cols=1)
hdr_cells7 = table7.rows[0].cells
hdr_cells7[0].text = rest_text[31]
set_cell_border(
hdr_cells7[0],
top={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"},
bottom={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"},
left={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"},
right={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"},
insideH={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"},
end={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"}
)
# T32 = document.add_paragraph(rest_text[32], style=style2)
T32 = document.add_heading('', level=2)
run_32 = T32.add_run(rest_text[32])
run_32.font.name = 'Times New Roman'
run_32.element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')
run_32.font.color.rgb = RGBColor(0, 0, 0)
# 小三
run_32.font.size = Pt(15)
run_32.font.bold = True
table8 = document.add_table(rows=1, cols=1)
hdr_cells8 = table8.rows[0].cells
hdr_cells8[0].text = rest_text[33]
set_cell_border(
hdr_cells8[0],
top={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"},
bottom={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"},
left={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"},
right={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"},
insideH={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"},
end={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"}
)
# 威胁情报
# T34 = document.add_paragraph(rest_text[34], style=style)
T34 = document.add_heading('', level=1)
run_34 = T34.add_run(rest_text[34])
run_34.font.name = 'Times New Roman'
run_34.element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')
run_34.font.color.rgb = RGBColor(0, 0, 0)
# 小三
run_34.font.size = Pt(22)
run_34.font.bold = True
# T35 = document.add_paragraph(rest_text[35], style=style2)
T35 = document.add_heading('', level=2)
run_35 = T35.add_run(rest_text[35])
run_35.font.name = 'Times New Roman'
run_35.element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')
run_35.font.color.rgb = RGBColor(0, 0, 0)
# 小三
run_35.font.size = Pt(15)
run_35.font.bold = True
table9 = document.add_table(rows=1, cols=1)
hdr_cells9 = table9.rows[0].cells
hdr_cells9[0].text = rest_text[36]
set_cell_border(
hdr_cells9[0],
top={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"},
bottom={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"},
left={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"},
right={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"},
insideH={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"},
end={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"}
)
# T37 = document.add_paragraph(rest_text[37], style=style2)
T37 = document.add_heading('', level=2)
run_37 = T37.add_run(rest_text[37])
run_37.font.name = 'Times New Roman'
run_37.element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')
run_37.font.color.rgb = RGBColor(0, 0, 0)
# 小三
run_37.font.size = Pt(15)
run_37.font.bold = True
table10 = document.add_table(rows=1, cols=1)
hdr_cells10 = table10.rows[0].cells
hdr_cells10[0].text = rest_text[38]
set_cell_border(
hdr_cells10[0],
top={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"},
bottom={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"},
left={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"},
right={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"},
insideH={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"},
end={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"}
)
# T39 = document.add_paragraph(rest_text[39], style=style2)
T39 = document.add_heading('', level=2)
run_39 = T39.add_run(rest_text[39])
run_39.font.name = 'Times New Roman'
run_39.element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')
run_39.font.color.rgb = RGBColor(0, 0, 0)
# 小三
run_39.font.size = Pt(15)
run_39.font.bold = True
table11 = document.add_table(rows=1, cols=1)
hdr_cells11 = table11.rows[0].cells
hdr_cells11[0].text = rest_text[40]
set_cell_border(
hdr_cells11[0],
top={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"},
bottom={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"},
left={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"},
right={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"},
insideH={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"},
end={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"}
)
# T41 = document.add_paragraph(rest_text[41], style=style2)
T41 = document.add_heading('', level=2)
run_41 = T41.add_run(rest_text[41])
run_41.font.name = 'Times New Roman'
run_41.element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')
run_41.font.color.rgb = RGBColor(0, 0, 0)
# 小三
run_41.font.size = Pt(15)
run_41.font.bold = True
table12 = document.add_table(rows=1, cols=1)
hdr_cells12 = table12.rows[0].cells
hdr_cells12[0].text = rest_text[42]
set_cell_border(
hdr_cells12[0],
top={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"},
bottom={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"},
left={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"},
right={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"},
insideH={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"},
end={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"}
)
# #生成所有表样式
# for s in styles:
# if s.type == WD_STYLE_TYPE.TABLE:
# document.add_paragraph("Table style is : "+ s.name)
# document.add_table(3,3, style = s)
# document.add_paragraph("\n")
# #生成所有段落样式
# for s in styles:
# if s.type == WD_STYLE_TYPE.PARAGRAPH:
# document.add_paragraph('Paragraph style is : '+ s.name, style = s)
#current_dir = os.path.dirname(os.path.abspath(__file__))
hostname = getpass.getuser()
print(hostname)
# 保存.docx文档
document.save("C://Users/"+hostname+"\Desktop\通用安全通告{0}.docx".format(date1))
#current_dir = os.path.dirname(os.path.realpath(sys.executable))
file_mulu = "C://Users/"+hostname+"\Desktop\通用安全通告{0}.docx".format(date1)
print(file_mulu)
document_read = Document(file_mulu)
# 获取所有段落
all_paragraphs = document.paragraphs
i = 1
for paragraph in all_paragraphs:
print(i, paragraph.text)
i = i + 1
print(file_mulu)
return file_mulu
# start()
mulu 指定位置插入目录
from win32com import client
from test import start
def mulu_sc(file):
print("开始生成目录")
word = client.DispatchEx("Word.Application")
word.Visible = 1 # 设置应用可见
word.DisplayAlerts = 0
doc= word.Documents.Open(file) # 使用微软office打开word
#doc.Range(Start=0, End=0).InsertBreak()
#doc.Paragraphs(1).Range.InsertBreak() #第一段插入分页
#print(doc.Range(Start=0, End=20))
#doc.Range(Start=0, End=0).InsertParagraphBefore() # 在首行之前插入一行,用于插入目录
#doc.Range(Start=0, End=100).InsertAfter('目录1')
FirstLineRange = doc.Paragraphs(14).Range # 指向新插入的行***空一行
FirstLineRange.Text = '目录'
FirstLineRange.Font.Bold =True
FirstLineRange.Font.Size = 20
FirstLineRange.Font.Name = '仿宋'
FirstLineRange.ParagraphFormat.Alignment =1
FirstLineRange.InsertParagraphAfter()
FirstLineRange.InsertParagraphAfter()
SecondLineRange = doc.Paragraphs(15).Range
doc.TablesOfContents.Add(Range=SecondLineRange,UseHeadingStyles=False,IncludePageNumbers=False, LowerHeadingLevel=0) # 生成目录对象
print(SecondLineRange.Text)
name = file
print(name)
doc.SaveAs(name, 12)
doc.Close(SaveChanges=False)
#doc.Close
word.Quit()
# a = start()
# mulu_sc(a)
main主函数
from mulu import mulu_sc
from test import start
def main():
a = start()
print("内容报告生成完毕")
mulu_sc(a)
print("目录生成完毕")
if __name__ == '__main__':
main()
1.txt内写入一周网安动态链接、
chromedriver.exe为selenium 所用驱动可以根据自己的chrome版本去下载:
https://registry.npmmirror.com/binary.html?path=chromedriver/
main.exe、chromedriver.exe、1.txt放在同一目录双击main.exe运行,生成的docx在桌面。
PS:由于打包exe之后运行路径和绝对路劲的问题,指定路劲为:
file_mulu = "C://Users/"+hostname+"\Desktop\通用安全通告{0}.docx".format(date1)
运行效果:
上述为全部源码:
需要exe的可以公众号回复:“一周网安动态”获取
作者:那酒不要留
-------------------------------------------
个性签名:独学而无友,则孤陋而寡闻。做一个灵魂有趣的人!知识源于分享!
如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!