欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python实现Office自动化办公(实现Python对word基本操作、对excel基本操作、在word中插入表格、word中插入网站爬虫取图)

程序员文章站 2022-06-27 14:37:48
Python实现Office办公自动化Pycharm 2018(Mac版)的下载功能快捷键如何插入一段漂亮的代码片生成一个适合你的列表创建一个表格设定内容居中、居左、居右SmartyPants创建一个自定义列表如何创建一个注脚注释也是必不可少的KaTeX数学公式新的甘特图功能,丰富你的文章UML 图表FLowchart流程图导出与导入导出导入Pycharm 2018(Mac版)的下载如果初次使用 Pycharm ,首先要知道PyCharm是一种Python IDE(IDE,全称Integrated D...

一、 Pycharm 2018(Mac版)的下载

Python实现Office自动化办公(实现Python对word基本操作、对excel基本操作、在word中插入表格、word中插入网站爬虫取图)

  1. PyCharm简介
    如果您初次使用 Pycharm ,首先要知道PyCharm是一种Python IDE(IDE,全称Integrated Development Environment ,集成开发环境,是用于提供程序开发环境的应用程序)。Pycharm是一个跨平台的IDE,可在Windows,macOS和Linux操作系统上提供一致的体验。PyCharm带有一整套可以帮助用户在使用Python语言开发时提高其效率的工具,比如调试、语法高亮、Project管理、代码跳转、智能提示、自动完成、单元测试、版本控制。
  2. PyCharm的下载安装
    Pycharm安装包推荐选择在官方网站http://www.jetbrains.com/pycharm/ 下载,其他网址下载方式可能相对繁琐而且有限速(通常是网盘下载,速度非常慢,至少花费1h),所以推荐官网下载;
    Python实现Office自动化办公(实现Python对word基本操作、对excel基本操作、在word中插入表格、word中插入网站爬虫取图)
    点击DOWNOAD,选择合适自己电脑的Pycharm版本,再点击Other versions,选择2018年的版本;Python实现Office自动化办公(实现Python对word基本操作、对excel基本操作、在word中插入表格、word中插入网站爬虫取图)
    选择“2018.2.8 - macOS (dmg)”进行下载,我们首选专业版。在专业版中,可以开发Django,Flask和Pyramid应用程序。此外,它完全支持HTML(包括HTML5),CSS,JavaScript和XML:这些语言通过插件捆绑在IDE中,并且默认情况下会为您打开。
    Python实现Office自动化办公(实现Python对word基本操作、对excel基本操作、在word中插入表格、word中插入网站爬虫取图)
    大约几分钟的时间可以下载完成,得到下载好的“pycharm-professional-2018.2.8.dmg”文件。(当然这里也提供了一个“pycharm-professional-2018.2.8.dmg”文件的网盘下载链接:https://cloud.189.cn/t/qmuuInrURz6f(访问码:g86u))
    下载完成后,点击dmg文件,再将左侧的Pycharm图标拖入右侧Applications文件夹里实现安装。Python实现Office自动化办公(实现Python对word基本操作、对excel基本操作、在word中插入表格、word中插入网站爬虫取图)
  3. PyCharm的激活
    暂且先不要打开软件。我们要先试着去激活它!
    先下载一个激活补丁:“JetbrainsCrack-3.1-release-enc.jar”(大小1M左右),下载地址:https://cloud.189.cn/t/AbmqaqYbEVVj(访问码:2kd1)。得到jar文件后,将它放到Pycharm的bin文件夹里面(存放地址不唯一,记住它的地址就行)。
    1.打开“访达”,选择“应用程序”-“Pycharm”,双指轻点PycharmPython实现Office自动化办公(实现Python对word基本操作、对excel基本操作、在word中插入表格、word中插入网站爬虫取图)
    再点击“显示包内容“,将“JetbrainsCrack-3.1-release-enc.jar”放进“bin”文件夹里面。
    Python实现Office自动化办公(实现Python对word基本操作、对excel基本操作、在word中插入表格、word中插入网站爬虫取图)
    同样在这个bin文件夹里,文本打开”pycharm.vmoptions“的文件Python实现Office自动化办公(实现Python对word基本操作、对excel基本操作、在word中插入表格、word中插入网站爬虫取图)
    在pycharm.vmoptions中最后另加一行代码:-javaagent:/Applications/PyCharm.app/Contents/bin/JetbrainsCrack-3.1-release-enc.jar
    修改完保存关闭文件。(这里的“/Applications/PyCharm.app/Contents/bin/JetbrainsCrack-3.1-release-enc.jar”就是JetbrainsCrack-3.1-release-enc.jar的绝对地址,不唯一。)
    Python实现Office自动化办公(实现Python对word基本操作、对excel基本操作、在word中插入表格、word中插入网站爬虫取图)
    然后打开Pycharm软件,进入激活步骤。选择激活码激活,输入如下内容激活:
ThisCrackLicenseId-{ 
 
“licenseId”:”ThisCrackLicenseId”, 
 
“licenseeName”:”idea”, 
 
“assigneeName”:”“, 
 
“assigneeEmail”:”idea@163.com”, 
 
“licenseRestriction”:”For This Crack, Only Test! Please support genuine!!!”, 
 
“checkConcurrentUse”:false, 
 
“products”:[ 
 
{“code”:”II”,”paidUpTo”:”2099-12-31”}, 
 
{“code”:”DM”,”paidUpTo”:”2099-12-31”}, 
 
{“code”:”AC”,”paidUpTo”:”2099-12-31”}, 
 
{“code”:”RS0”,”paidUpTo”:”2099-12-31”}, 
 
{“code”:”WS”,”paidUpTo”:”2099-12-31”}, 
 
{“code”:”DPN”,”paidUpTo”:”2099-12-31”}, 
 
{“code”:”RC”,”paidUpTo”:”2099-12-31”}, 
 
{“code”:”PS”,”paidUpTo”:”2099-12-31”}, 
 
{“code”:”DC”,”paidUpTo”:”2099-12-31”}, 
 
{“code”:”RM”,”paidUpTo”:”2099-12-31”}, 
 
{“code”:”CL”,”paidUpTo”:”2099-12-31”}, 
 
{“code”:”PC”,”paidUpTo”:”2099-12-31”} 
 
], 
 
“hash”:”2911276/0”, 
 
“gracePeriodDays”:7, 
 
“autoProlongated”:false} 

随后进入Pycharm的界面!
Python实现Office自动化办公(实现Python对word基本操作、对excel基本操作、在word中插入表格、word中插入网站爬虫取图)

二、Welcome to PyCharm

Python实现Office自动化办公(实现Python对word基本操作、对excel基本操作、在word中插入表格、word中插入网站爬虫取图)

  1. 什么是Virtualenv
    点击“Create New Project”,选择“Pure Python”,会出现“New environment using”默认“Virtualenv”。Pycharm可以指定解释器。什么是“Virtualenv”,在操作系统下,我们会安装很多版本的Python解释器。同样,我们也有可能因为各种原因,需要不同版本的模块。virtualenv就是用来为一个应用创建一套“隔离”的Python运行环境。在Python3.4之前,virtualenv需要独立手动安装后,方可执行。之后的版本中,Python自带了一个venv库,执行Python –m venv就可以达到与virtalenv同样的效果。更多的用户习惯上还是使用virtualenv的方式。
    virtualenv可以指定虚拟的Python解释器版本,只要你本地操作系统下安装了这个版本。
  2. 选择项目解释器
    点击“Project Interpreter”(项目解释器),在“Base interpreter”处选择python的要使用的虚拟环境。选择python的版本。再然后点击“Create”进行创建。Python实现Office自动化办公(实现Python对word基本操作、对excel基本操作、在word中插入表格、word中插入网站爬虫取图)
    如何选择Python的版本(如果您已经安装了Python3)。首先打开终端,输入whereis python3,得到如下地址/usr/bin/python3
    Python实现Office自动化办公(实现Python对word基本操作、对excel基本操作、在word中插入表格、word中插入网站爬虫取图)

PS:Mac终端快捷键大全:
Ctrl+c:中断终端中正在执行的任务。
Esc键:连续按3次显示所有的支持的终端命令。
Ctrl+p:上一个使用的历史命令。 (p:previous)
Ctrl+n:下一个使用的历史命令。(n:next)
Ctrl + d:相信这个快捷键在很多交换式情景下都能退出交换式模式。

随即进入Pycharm的开发界面。
Python实现Office自动化办公(实现Python对word基本操作、对excel基本操作、在word中插入表格、word中插入网站爬虫取图)

  1. 新建python代码
    点击File–New…–Python File
    Python实现Office自动化办公(实现Python对word基本操作、对excel基本操作、在word中插入表格、word中插入网站爬虫取图)
    Python实现Office自动化办公(实现Python对word基本操作、对excel基本操作、在word中插入表格、word中插入网站爬虫取图)
    输入python文件名,点击OK。
    Python实现Office自动化办公(实现Python对word基本操作、对excel基本操作、在word中插入表格、word中插入网站爬虫取图)
    打开pycharm,新建python项目时,提示“no python interpreter configured for the project”,理解为没有找到解释器,可手动设置添加解释器。1. 点击Configure Python interpreter,进入project interpreter设置页面。
    Python实现Office自动化办公(实现Python对word基本操作、对excel基本操作、在word中插入表格、word中插入网站爬虫取图)
    点击“Show All”
    Python实现Office自动化办公(实现Python对word基本操作、对excel基本操作、在word中插入表格、word中插入网站爬虫取图)
    Python实现Office自动化办公(实现Python对word基本操作、对excel基本操作、在word中插入表格、word中插入网站爬虫取图)

Python实现Office自动化办公(实现Python对word基本操作、对excel基本操作、在word中插入表格、word中插入网站爬虫取图)
Python实现Office自动化办公(实现Python对word基本操作、对excel基本操作、在word中插入表格、word中插入网站爬虫取图)
Python实现Office自动化办公(实现Python对word基本操作、对excel基本操作、在word中插入表格、word中插入网站爬虫取图)
会出现很多的包,例如:Django、Jinja2等
Python实现Office自动化办公(实现Python对word基本操作、对excel基本操作、在word中插入表格、word中插入网站爬虫取图)
如果有需要的python包可以添加下载。本项目需要用到的有:docxtpl、math、openpyxl、docx、os、jinja2、selenium、time、re、pandas等等(这是代码里面的名称)
导入库

from docxtpl import DocxTemplate
import os

Python字符串与变量(出于商业隐私,相关信息暂时删去,可以参考代码修改成自己所需)

Equipment_prices=0#设备部分限价
Service_prices=0 #服务部分限价
Total_prices=Equipment_prices+Service_prices #项目总价
Ne=0#设备数量
Ns=0 #服务数量(除了集成服务费、安全生产费两个之外的数量,一般可为0)

Branch=""#某某支局
Address="" #地址
Company_name= "" #公司名称
Legal_representative="" #法人
time_limit_for_a_project=""  #工期
warranty_period=""   #维保期
type_of_payment= ""#付款条件
Account_Bank= "" #开户行
Bank_account=""#银行帐户
Tax_identification_number=""#纳税人识别号#
Equipment_and_related_integration_services=""#设备材料服务等等
Amount_of_performance="" #业绩金额(万元)
Time="2020年 月 日" # 询价文件发布时间
Qua23=""  #  2.3资质条件,例如安全生产许可证等等
Qua24=""#电工证等等
Application_materials=""#报名所需材料
T31="2020 年 月日17:30:00(北京时间,下同)至 2020 年 月日17:00:00 。" #询价文件获取时间
Tax_included= ""#含税税率
Cost_item="" #合同主体

三、使用DocxTemplate功能模块,实现python对Office Word文档进行基本操作,批量修改模板。

首先要安装docxtpl:

Python实现Office自动化办公(实现Python对word基本操作、对excel基本操作、在word中插入表格、word中插入网站爬虫取图)
Python实现Office自动化办公(实现Python对word基本操作、对excel基本操作、在word中插入表格、word中插入网站爬虫取图)
Python实现Office自动化办公(实现Python对word基本操作、对excel基本操作、在word中插入表格、word中插入网站爬虫取图)
Python实现Office自动化办公(实现Python对word基本操作、对excel基本操作、在word中插入表格、word中插入网站爬虫取图)
选择使用清华镜像,速度会很快(https://pypi.tuna.tsinghua.edu.cn/simple
Python实现Office自动化办公(实现Python对word基本操作、对excel基本操作、在word中插入表格、word中插入网站爬虫取图)
Python实现Office自动化办公(实现Python对word基本操作、对excel基本操作、在word中插入表格、word中插入网站爬虫取图)
python里面DocxTemplate模块的使用:

doc = DocxTemplate("【询价文件】模板.docx")  # 对要操作的docx文档进行初始化
context = {'Address': Address,
           'Company_name': Company_name,
           'Legal_representative': Legal_representative,
           'time_limit_for_a_project': time_limit_for_a_project,
           'warranty_period':warranty_period,
           'type_of_payment':type_of_payment,
           'Account_Bank':Account_Bank,
           'Bank_account': Bank_account,
           'Tax_identification_number': Tax_identification_number,
           'Project_name':Project_name,
           'Equipment_and_related_integration_services':Equipment_and_related_integration_services,
           'Amount_of_performance':Amount_of_performance,
           'Equipment_prices':str(Equipment_prices),
           'Service_prices':str(Service_prices),
           'Total_prices':str(Total_prices),
           'Time':Time,
           'Qua23':Qua23,
           'Qua24':Qua24,
           'Application_materials':Application_materials,
           'T31':T31
          }  # company_name 是存在于1.docx文档里面的变量,就像这样{{**********}},直接放在1.docx文件的明确位置就行
doc.render(context)  # 这里是有jinjia2的模板语言进行变量的替换,然后便可以在1.docx文档里面看到{{*********}}变成了World company

以下是Word里面的模板形式:
Python实现Office自动化办公(实现Python对word基本操作、对excel基本操作、在word中插入表格、word中插入网站爬虫取图)
以“Project_name”为例,在python里下图的形式书写,与word模板进行对应。调整python里面字符串的内容,word里对于位置也随之变化。实现一个模板快速调整的功能。
Python实现Office自动化办公(实现Python对word基本操作、对excel基本操作、在word中插入表格、word中插入网站爬虫取图)
4、最后输出新的生成文件:


pwd = os.getcwd()
filename = pwd +'/'+str(Project_name)+"---询价文件"
print(filename)
if not os.path.exists(filename):               #判断文件夹是否存在
    os.makedirs(filename)                       #新建文件夹
else:
    print('文件夹已存在')

doc.save(str(filename)+"/"+str(Project_name+"【询价文件】.docx"))  # 保存

Python实现Office自动化办公(实现Python对word基本操作、对excel基本操作、在word中插入表格、word中插入网站爬虫取图)

四、实现Python对Office Excel表格文档进行自动化操作(添加行或列、增加内容、合并单元格等等):

Python实现Office自动化办公(实现Python对word基本操作、对excel基本操作、在word中插入表格、word中插入网站爬虫取图)

#coding=utf-8
import math
import Inquiry_Document
import docx
import openpyxl  # 导包读取
from docx import Document
from docx.enum.text import WD_ALIGN_PARAGRAPH
from docx.oxml.ns import qn
from docx.shared import Pt, RGBColor
from openpyxl import load_workbook
from openpyxl.styles import Font, Border, Side, PatternFill, Alignment

Project_name=Inquiry_Document. Project_name#项目名称
Equipment_prices=Inquiry_Document.Equipment_prices#设备部分限价
Ne=Inquiry_Document.Ne #设备数量
Service_prices=Inquiry_Document.Service_prices#服务部分限价
Ns=Inquiry_Document.Ns #服务数量(除了集成服务费、安全生产费两个之外的数量,一般可为0)
Total_prices=Inquiry_Document.Total_prices #项目总价

wb = openpyxl.load_workbook("【明细报价表】模板.xlsx") #加载表格模板文件
sheet1 = wb["报价汇总单"] #获取sheet1对象,修改单元格内容
sheet1["A1"] = Project_name+"--报价汇总单" #修改内容
sheet1["F4"] = Equipment_prices
sheet1["F5"] = Service_prices
sheet1["F6"] = Total_prices
sheet2 = wb["明细报价表"] #获取sheet2对象,修改单元格内容
sheet2["A1"] = Project_name+"--报价明细表"
sheet2.insert_rows(4,Ne-1) #添加设备行
A1="A3:"
A2="A"
A3=str(Ne+3)
A=A1+A2+A3
for i in range(Ne):
    B='B'+str(3+i)
    J='J'+str(3+i)
    sheet2[B]=i+1  #添加设备序号
    sheet2[J]="13%"  #添加设备税率
sheet2.merge_cells(A) #合并单元格

B="B"+str(Ne+3)+":I"+str(Ne+3)
sheet2["B"+str(Ne+3)] ="合计(最高限价"+str(Equipment_prices)+"元)"
sheet2.merge_cells(B) #合并单元格
def set_cells(cells,type,color=None):
  aligncenter=Alignment(horizontal='center',vertical='center')  #居中
  sidestyle=Side(border_style='thin')
  border=Border(left=sidestyle,right=sidestyle,top=sidestyle,bottom=sidestyle)
  for i in cells:
    for j in i:
      if(type=='percent'):
        j.number_format='0.00%'
      elif(type=='bold'):
        j.style='Pandas'
      elif(type=='center'):
        j.alignment=aligncenter
      elif(type=='border'):
        j.border=border
      elif(type=='color'):
      	j.fill = PatternFill("solid", fgColor=color)


sheet2.insert_rows(Ne+5,Ns) #添加设备行
for j in range(Ns+2):
    B1='B'+str(Ne+4+j)
    J='J'+str(Ne+4+j)
    sheet2[B1]=j+Ne+1  #添加设备序号
    sheet2[J] = "6%"


A4="A"+str(Ne+4)+":A"+str(Ne+Ns+6)
A5="B"+str(Ne+Ns+6)+":I"+str(Ne+Ns+6)
A6="A"+str(Ne+Ns+7)+":J"+str(Ne+Ns+7)
sheet2.merge_cells(A4) #合并单元格
sheet2.merge_cells(A5) #合并单元格
sheet2.merge_cells(A6) #合并单元格
sheet2["N"+str(Ne+3)]=Equipment_prices
sheet2["N"+str(Ne+Ns+6)]=Service_prices


sheet2["B"+str(Ne+3)]="合计(最高限价"+str(Equipment_prices)+"元)"
sheet2["B"+str(Ne+Ns+6)]="合计(最高限价"+str(Service_prices)+"元)"
sheet2["A"+str(Ne+Ns+7)]="含增值税总计(最高限价"+str(Total_prices)+"元)"
sheet2["M"+str(Ne+Ns+5)]=math.ceil(Service_prices*0.015)
sheet2["L"+str(Ne+Ns+5)]=round(((math.ceil(Service_prices*0.015))/1.06),2)
sheet2["K"+str(Ne+Ns+5)]=math.ceil(Service_prices*0.015)
sheet2["I"+str(Ne+Ns+5)]=round(((math.ceil(Service_prices*0.015))/1.06),2)
All='A1:N'+str(Ne+Ns+10)

A7="A"+str(Ne+Ns+8)+":N"+str(Ne+Ns+8)
A8="A"+str(Ne+Ns+9)+":N"+str(Ne+Ns+9)
A9="A"+str(Ne+Ns+10)+":N"+str(Ne+Ns+10)
sheet2.merge_cells(A7) #合并单元格
sheet2.merge_cells(A8) #合并单元格
sheet2.merge_cells(A9) #合并单元格
cells=sheet2[All]
cells1=sheet2['A1:N'+str(Ne+Ns+7)]
set_cells(cells1,'center') # 居中

set_cells(sheet1['A1:F6'],'border') #sheet1 加边框
set_cells(cells,'border') # sheet2 加边框

for i in range(3,Ns+Ne+5):
  sheet2['B'+str(i)].font = Font(name=u'宋体', bold=True, italic=False, size=14)  # bold是否为粗体。italic是否为斜体。
  #sheet2['J' + str(i)].font = Font(name=u'宋体', bold=True, italic=False, size=14)  # bold是否为粗体。italic是否为斜体。
sheet2['B'+str(Ne+3)].font = Font(name=u'等线 (正文)', bold=True, italic=False, size=12)  # bold是否为粗体。italic是否为斜体。
wb.save(Inquiry_Document.filename+"/"+str(Inquiry_Document.Project_name)+"【明细报价表】.xlsx") #另存为文件名

五、实现Python对Office word文档进行添加表格的自动化操作:

mport docx
from docx import Document
from docx.enum.text import WD_ALIGN_PARAGRAPH
from docx.oxml.ns import qn
from docx.shared import Pt, RGBColor
from openpyxl import load_workbook
import Inquiry_Document
Project_name=Inquiry_Document.Project_name #项目名称
Equipment_prices=Inquiry_Document.Equipment_prices#设备部分限价
Ne=Inquiry_Document.Ne #设备数量
Service_prices=Inquiry_Document.Service_prices #服务部分限价
Ns=Inquiry_Document.Ns #服务数量(除了集成服务费、安全生产费两个之外的数量,一般可为0)
Total_prices=Equipment_prices+Service_prices #项目总价

def xlsx2docx(fn):
   wb=load_workbook(fn,data_only=True)
   document=Document()

   for ws in wb.worksheets:
       rows=list(ws.rows)
       #document.add_paragraph(ws.title)
       #document.add_paragraph("标题")
       #paragraph = document.add_paragraph(str(Project_name) + "技术规范书")
       #paragraph_format = paragraph.paragraph_format
       #paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER

       Head1 = document.add_heading("", level=2)  # 这里不填标题内容    "*****技术规范书" 大标题
       run = Head1.add_run(str(Project_name) + "技术规范书")
       run.font.name = u'Cambria'
       run._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')
       run.font.color.rgb = RGBColor(0, 0, 0)
       Head1_format = Head1.paragraph_format
       Head1_format.alignment = WD_ALIGN_PARAGRAPH.CENTER

       # 标题等级如1,2,3这些数字,一级标题二级标题这样
       Head2= document.add_heading("", level=2)  # 这里不填标题内容     "下表为本项目采购清单及相关参数要求:"
       run = Head2.add_run('下表为本项目采购清单及相关参数要求:')
       run.font.name = u'Cambria'
       run._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')
       run.font.color.rgb = RGBColor(0, 0, 0)

       table=document.add_table(rows=len(rows),
                                cols=len(rows[0]),
                                style='Table Grid')

       for irow,row in enumerate(rows):                      #      制作清单表格
           for icol,col in enumerate(row):
               table.cell(irow,icol).text=str(col.value)
   for i, p in enumerate(document.paragraphs):
       print(str(i) + ": " + str(p.text))
   document.styles['Normal'].font.name = u'宋体'
   document.styles['Normal']._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')
   document.styles['Normal'].font.size = Pt(10.5)
   document.styles['Normal'].font.color.rgb = RGBColor(0, 0, 0)
   #document = document.add_paragraph()
   file = docx.Document('文字模板.docx')
   for para in file.paragraphs:
       document.add_paragraph(para.text)
   document.save(Inquiry_Document.filename+"/"+str(Inquiry_Document.Project_name)+"【技术规范书】.docx")
xlsx2docx('清单表格模板.xlsx')

六、实现Python对一般网页进行爬虫取图或取数据并导入Word文档中的自动化操作(实现在“信用中国”网站上自动爬取三家公司的信用信息查询的截图,并且导入Word文档的特定位置):

运用了selenium三方库,可以实现让浏览器完成自动化的操作。

首先要安装浏览器驱动driver,选择一种即可,我使用了Firefox的驱动geckodriver
1.chromedriver 下载地址:https://code.google.com/p/chromedriver/downloads/list
2.Firefox的驱动geckodriver 下载地址:https://github.com/mozilla/geckodriver/releases/
3.IE的驱动IEdriver 下载地址:http://www.nuget.org/packages/Selenium.WebDriver.IEDriver/

# 从火狐浏览器中,爬取"信用中国"中相关公司的信用信息并截图,这是爬取网页截图部分
from selenium.webdriver import Firefox
from selenium.webdriver import FirefoxOptions
from selenium.webdriver.common.keys import Keys
from time import sleep
import Basic_Info
import time,re
option = FirefoxOptions()
option.add_argument("--headless")  # 隐藏浏览器
browser=Firefox(executable_path=r'/usr/local/bin/geckodriver',options=option)
url1="https://www.creditchina.gov.cn/xinyongxinxixiangqing/xyDetail.html?searchState=1&entityType=1&keyword="+str(Basic_Info.company1)
url2="https://www.creditchina.gov.cn/xinyongxinxixiangqing/xyDetail.html?searchState=1&entityType=1&keyword="+str(Basic_Info.company2)
url3="https://www.creditchina.gov.cn/xinyongxinxixiangqing/xyDetail.html?searchState=1&entityType=1&keyword="+str(Basic_Info.company3)
#url = "https://www.creditchina.gov.cn/xinyongxinxi/index.html?index=0&scenes=defaultScenario&tableName=credit_xyzx_tyshxydm&searchState=2&entityType=1,2,4,5,6,7,8&keyword=%20"
browser.get(url1)
#browser.find_element_by_id("search_input").clear()
#browser.find_element_by_id('search_input').send_keys("股份有限公司")
#browser.find_elements_by_css_selector(".search_btn")[0].click()
#elements[0].click()
#sleep(6)
#sreach_window=browser.current_window_handle
#browser.find_element_by_xpath("/html/body/div[2]/div/div[1]/div[3]/ul[1]/li[1]").click()
#html = browser.page_source
#sreach_window1=browser.current_window_handle
sleep(5)
browser.set_window_size(1380,1100) #设置为固定分辨率大小
browser.get_screenshot_as_file("screenshot_company1.png")#截图
browser.get(url2)
sleep(5)
browser.set_window_size(1380,1100) #设置为固定分辨率大小
browser.get_screenshot_as_file("screenshot_company2.png")#截图
browser.get(url3)
sleep(5)
browser.set_window_size(1380,1100) #设置为固定分辨率大小
browser.get_screenshot_as_file("screenshot_company3.png")#截图

完整的代码:

#coding=utf-8
import math
import os
import Basic_Info
import docx
import openpyxl  # 导包读取
from docxtpl import DocxTemplate
from docxtpl import InlineImage
from docx.shared import Mm
import jinja2
from docx import Document
from docx.enum.text import WD_ALIGN_PARAGRAPH
from docx.oxml.ns import qn
from docx.shared import Pt, RGBColor
from openpyxl import load_workbook
from openpyxl.styles import Font, Border, Side, PatternFill, Alignment
Project_name=Basic_Info. Project_name#项目名称
Equipment_prices=Basic_Info.Equipment_prices#设备部分限价
Ne=Basic_Info.Ne #设备数量
Service_prices=Basic_Info.Service_prices#服务部分限价
Ns=Basic_Info.Ns #服务数量(除了集成服务费、安全生产费两个之外的数量,一般可为0)
Total_prices=Basic_Info.Total_prices #项目总价
# 从火狐浏览器中,爬取"信用中国"中相关公司的信用信息并截图
from selenium.webdriver import Firefox
from selenium.webdriver import FirefoxOptions
from selenium.webdriver.common.keys import Keys
from time import sleep
import Basic_Info
import time,re
option = FirefoxOptions()
option.add_argument("--headless")  # 隐藏浏览器
browser=Firefox(executable_path=r'/usr/local/bin/geckodriver',options=option)
url1="https://www.creditchina.gov.cn/xinyongxinxixiangqing/xyDetail.html?searchState=1&entityType=1&keyword="+str(Basic_Info.company1)
url2="https://www.creditchina.gov.cn/xinyongxinxixiangqing/xyDetail.html?searchState=1&entityType=1&keyword="+str(Basic_Info.company2)
url3="https://www.creditchina.gov.cn/xinyongxinxixiangqing/xyDetail.html?searchState=1&entityType=1&keyword="+str(Basic_Info.company3)
#url = "https://www.creditchina.gov.cn/xinyongxinxi/index.html?index=0&scenes=defaultScenario&tableName=credit_xyzx_tyshxydm&searchState=2&entityType=1,2,4,5,6,7,8&keyword=%20"
browser.get(url1)
#browser.find_element_by_id("search_input").clear()
#browser.find_element_by_id('search_input').send_keys("股份有限公司")
#browser.find_elements_by_css_selector(".search_btn")[0].click()
#elements[0].click()
#sleep(6)
#sreach_window=browser.current_window_handle
#browser.find_element_by_xpath("/html/body/div[2]/div/div[1]/div[3]/ul[1]/li[1]").click()
#html = browser.page_source
#sreach_window1=browser.current_window_handle
sleep(5)
browser.set_window_size(1380,1100) #设置为固定分辨率大小
browser.get_screenshot_as_file("screenshot_company1.png")
browser.get(url2)
sleep(5)
browser.set_window_size(1380,1100) #设置为固定分辨率大小
browser.get_screenshot_as_file("screenshot_company2.png")
browser.get(url3)
sleep(5)
browser.set_window_size(1380,1100) #设置为固定分辨率大小
browser.get_screenshot_as_file("screenshot_company3.png")
wb = openpyxl.load_workbook("模版文件/Procurement_Results_Form_Template.xlsx")       #加载表格模板文件
sheet1 = wb["模版"] #获取sheet1模版对象,修改单元格内容
sheet1["A2"] = "项目名称:"+Project_name # 项目名称
sheet1["A3"] = "询价编号:"+Basic_Info.Inquiry_number #询价编号
sheet1["A4"]="日期:"+Basic_Info.Time
sheet1["A5"]="唱价时间:"+Basic_Info.Bid_opening_time_detailedness
sheet1["A8"]=Basic_Info.company1
sheet1["A9"]=Basic_Info.company2
sheet1["A10"]=Basic_Info.company3
sheet1["A13"]="询价文件发布时间:"+Basic_Info.Time
sheet1["A14"]="行贿信息与信用信息查询时间:"+Basic_Info.Bid_opening_time_1
sheet1["A15"]="结果编制时间:"+Basic_Info.Bid_opening_time
sheet1["A17"]=Total_prices
sheet1["A18"]=Basic_Info.time_limit_for_a_project
sheet1["A19"]=Basic_Info.warranty_period
sheet1["A20"]=Basic_Info.T31
sheet1["A21"]=Total_prices
sheet1["A22"]=0 #设备最高限价=0
sheet1["A23"]=Service_prices
sheet1["A24"]=Basic_Info.Company_name
sheet1["A25"]=Basic_Info.Legal_representative
sheet1["A26"]=Basic_Info.Group_leader
sheet1["A27"]=Basic_Info.Group_member
sheet1["A28"]=Basic_Info.Buyer_representative
sheet1["A29"]=Basic_Info.phone_number_1
sheet1["A30"]=Basic_Info.phone_number_2
sheet1["A31"]=Basic_Info.phone_number_3
sheet1["A32"]=Basic_Info.RAR1
sheet1["A33"]=Basic_Info.RAR1_phone
sheet1["A34"]=Basic_Info.RAR1_mail
sheet1["A35"]=Basic_Info.RAR2
sheet1["A36"]=Basic_Info.RAR2_phone
sheet1["A37"]=Basic_Info.RAR2_mail
sheet1["A38"]=Basic_Info.RAR3
sheet1["A39"]=Basic_Info.RAR3_phone
sheet1["A40"]=Basic_Info.RAR3_mail
sheet1["A41"]=Basic_Info.RAR1_total_prices
sheet1["A42"]=Basic_Info.RAR2_total_prices
sheet1["A43"]=Basic_Info.RAR3_total_prices

wb1 = openpyxl.load_workbook("模版文件/Credit_check_of_MSS.xlsx")     #加载MSS信用核查表格模板文件
sheet2 = wb1["信用管理"] #获取sheet2模版对象,修改单元格内容
sheet2["C2"]=Basic_Info.company1
sheet2["C3"]=Basic_Info.company2
sheet2["C4"]=Basic_Info.company3
sheet2["D2"]=Basic_Info.company1_legal_person
sheet2["D3"]=Basic_Info.company2_legal_person
sheet2["D4"]=Basic_Info.company3_legal_person
sheet2["E2"]=Basic_Info.company1_Registered_capital
sheet2["E3"]=Basic_Info.company2_Registered_capital
sheet2["E4"]=Basic_Info.company3_Registered_capital
sheet2["F2"]=Basic_Info.company1_Date_of_establishment
sheet2["F3"]=Basic_Info.company2_Date_of_establishment
sheet2["F4"]=Basic_Info.company3_Date_of_establishment
sheet2["G2"]=Basic_Info.company1_Operating_status
sheet2["G3"]=Basic_Info.company2_Operating_status
sheet2["G4"]=Basic_Info.company3_Operating_status
sheet2["H2"]=Basic_Info.company1_unified_social_credit_code
sheet2["H3"]=Basic_Info.company2_unified_social_credit_code
sheet2["H4"]=Basic_Info.company3_unified_social_credit_code
sheet2["I2"]=Basic_Info.company1_type_of_company
sheet2["I3"]=Basic_Info.company1_type_of_company
sheet2["I4"]=Basic_Info.company1_type_of_company
sheet2["J2"]=Basic_Info.company1_operating_period
sheet2["J3"]=Basic_Info.company2_operating_period
sheet2["J4"]=Basic_Info.company3_operating_period
sheet2["K2"]=Basic_Info.company1_used_name
sheet2["K3"]=Basic_Info.company2_used_name
sheet2["K4"]=Basic_Info.company3_used_name
sheet2["M2"]=Basic_Info.company1_total_legal_proceedings
sheet2["M3"]=Basic_Info.company2_total_legal_proceedings
sheet2["M4"]=Basic_Info.company3_total_legal_proceedings

doc = DocxTemplate("模版文件/评审报告.docx")  # 对要操作的docx文档进行初始化
context = {
           "Project_name":Project_name,
           'Company_name':Basic_Info.Company_name,
           'Bid_opening_time':Basic_Info.Bid_opening_time,
           'Total_prices':Basic_Info.Total_prices,
           'Time':Basic_Info.Time,
           'T31':Basic_Info.T31,
           'Bid_opening_time':Basic_Info.Bid_opening_time,
           'Response_deadline':Basic_Info.Response_deadline,
           'Bid_opening_time_detailedness':Basic_Info.Bid_opening_time_detailedness,
           'company1':Basic_Info.company1,
           'company2':Basic_Info.company2,
           'company3':Basic_Info.company3,
           'rank_prices1':int(Basic_Info.rank_prices1),
           'rank_prices2':int(Basic_Info.rank_prices2),
           'rank_prices3':int(Basic_Info.rank_prices3),
           'RAR1_total_prices':Basic_Info.RAR1_total_prices,
           'RAR2_total_prices':Basic_Info.RAR2_total_prices,
           'RAR3_total_prices':Basic_Info.RAR3_total_prices,
          }  # company_name 是存在于1.docx文档里面的变量,就像这样{{**********}},直接放在1.docx文件的明确位置就行
doc.render(context)  #

doc1 = DocxTemplate("模版文件/附件5_6_9_10等材料.docx")  # 对要操作的docx文档进行初始化
context1 = {
           "Project_name":Project_name,
           'Company_name':Basic_Info.Company_name,
           'Bid_opening_time':Basic_Info.Bid_opening_time,
           'Bid_opening_time_1':Basic_Info.Bid_opening_time_1,
           'Amount_of_performance':Basic_Info.Amount_of_performance,
           'Total_prices':Basic_Info.Total_prices,
           'Qua23':Basic_Info.Qua23,
           'Qua24':Basic_Info.Qua24,
           'Time':Basic_Info.Time,
           'T31':Basic_Info.T31,
           'Inquiry_number':Basic_Info.Inquiry_number,
           'Response_deadline':Basic_Info.Response_deadline,
           'Bid_opening_time_detailedness':Basic_Info.Bid_opening_time_detailedness,
           'company1':Basic_Info.company1,
           'company2':Basic_Info.company2,
           'company3':Basic_Info.company3,
           'rank_prices1':int(Basic_Info.rank_prices1),
           'rank_prices2':int(Basic_Info.rank_prices2),
           'rank_prices3':int(Basic_Info.rank_prices3),
           'screenshot_company1': InlineImage(doc1, 'screenshot_company1.png', width=Mm(160), height=Mm(60)),
           'screenshot_company2': InlineImage(doc1, 'screenshot_company2.png',width=Mm(160), height=Mm(60)),
           'screenshot_company3': InlineImage(doc1, 'screenshot_company3.png',width=Mm(160), height=Mm(60))
          }  # company_name 是存在于1.docx文档里面的变量,就像这样{{**********}},直接放在1.docx文件的明确位置就行
doc1.render(context1)  #


doc2 = DocxTemplate("模版文件/采购结果签报.docx")  # 对要操作的docx文档进行初始化
context2 = {

           'Group_leader': Basic_Info.Group_leader,
           'Group_member': Basic_Info.Group_member,
           'Buyer_representative': Basic_Info.Buyer_representative,
           'RAR1_total_prices': Basic_Info.RAR1_total_prices,
           'RAR2_total_prices': Basic_Info.RAR2_total_prices,
           'RAR3_total_prices': Basic_Info.RAR3_total_prices,
           'Equipment_and_related_integration_services':Basic_Info.Equipment_and_related_integration_services,
           'Tax':Basic_Info.Tax,
           'RAR1_total_prices_Excluding_tax':Basic_Info.RAR1_total_prices_Excluding_tax,
           'RAR2_total_prices_Excluding_tax': Basic_Info.RAR2_total_prices_Excluding_tax,
           'RAR3_total_prices_Excluding_tax': Basic_Info.RAR3_total_prices_Excluding_tax,

           'Tax_included':Basic_Info.Tax_included,
           'Cost_item':Basic_Info.Cost_item,
           'Project_name':Project_name,
           'Company_name':Basic_Info.Company_name,
           'Bid_opening_time':Basic_Info.Bid_opening_time,
           'Bid_opening_time_1':Basic_Info.Bid_opening_time_1,
           'Amount_of_performance':Basic_Info.Amount_of_performance,
           'Total_prices':Basic_Info.Total_prices,
           'Inquiry_number':Basic_Info.Inquiry_number,
           'Response_deadline':Basic_Info.Response_deadline,
           'Bid_opening_time_detailedness':Basic_Info.Bid_opening_time_detailedness, #
           'company1':Basic_Info.company1,
           'company2':Basic_Info.company2,
           'company3':Basic_Info.company3,
           'rank_prices1':int(Basic_Info.rank_prices1),
           'rank_prices2':int(Basic_Info.rank_prices2),
           'rank_prices3':int(Basic_Info.rank_prices3),

          }  # company_name 是存在于1.docx文档里面的变量,就像这样{{**********}},直接放在1.docx文件的明确位置就行
doc2.render(context2)  doc.save(Basic_Info.filename+"/"+str(Basic_Info.Project_name)+"【评审报告】.docx") # 保存
doc1.save(Basic_Info.filename+"/"+str(Basic_Info.Project_name)+"---附件5_6_9等多个文件.docx") # 保存
wb.save(Basic_Info.filename+"/"+str(Basic_Info.Project_name)+"---多个附件表格.xlsx") #另存为文件名
wb1.save(Basic_Info.filename+"/"+str(Basic_Info.Project_name)+"---MSS信用核查.xlsx") #另存为文件名
doc2.save(Basic_Info.filename1+"/"+str(Basic_Info.Project_name)+"【采购结果签报】.docx") # 保存

最后的输出效果:
Python实现Office自动化办公(实现Python对word基本操作、对excel基本操作、在word中插入表格、word中插入网站爬虫取图)

七、实现GUI的搭建,可视化输入(暂未实现)

(正在搭建中,后续更新…)

八、云服务的使用,Python上云(暂未实现)

(正在搭建中,后续更新…)

九、搭建web网页,实现办公自动化系统(暂未实现)

(正在搭建中,后续更新…)

本文地址:https://blog.csdn.net/weixin_43756157/article/details/109822939