1. python-docx模块
- word的自动化
- 针对手动创建批量制式Word文件、修改现有大量word文件存在的共性问题
- python-docx是第三方模块,用于自动化生成和修改word文档
from docx import Document
from docx.shared import Pt,RGBColor
from docx.enum.style import WD_STYLE_TYPE
document = Document()
document.add_heading('我爱你', level=2)
style = document.styles.add_style('textstyle', WD_STYLE_TYPE.PARAGRAPH)
style.font.size = Pt(5)
p1 = document.add_paragraph('我爱你,像风走过了3000里! 如果某一天,你不喜欢我了,我希望先开口的人是我,而不是你。渣男不渣,只是他们的心碎成了很多片。',style='textstyle')
p1.insert_paragraph_before("baby, i want to say you :")
format = p1.paragraph_format
format.left_indent = Pt(20)
format.right_indent = Pt(20)
format.first_line_indent = Pt(20)
format.line_spacing = 1.5
run = p1.add_run('耳旁软语是你,声嘶力竭也是你。爱的是你,离开的也是你。曾共度两三年的是你,而今老死不相往来也是你。')
run.font.size = Pt(12)
run.font.name = '微软雅黑'
run.font.color.rgb=RGBColor(235,33,24)
run1 = p1.add_run('只要最后是你就好。')
run1.bold = True
run1.font.underline = True
run1.font.italic = True
document.add_picture('plant.png', Pt(20), Pt(30))
table = document.add_table(rows=1, cols=3, style='Medium List 1')
header_cells = table.rows[0].cells
header_cells[0].text = '地区'
header_cells[1].text = '最低彩礼'
header_cells[2].text = '最高彩礼'
data =(
["四川", 5, 15],
["江西", 30, 50],
["乐山", 0, 10],
)
for item in data:
rows_cells = table.add_row().cells
rows_cells[0].text = str(item[0])
rows_cells[1].text = str(item[1])
rows_cells[2].text = str(item[2])
print(len(document.tables[0].rows))
print(len(document.tables[0].columns))
print(document.tables[0].cell(0,2).text)
document.save('test.docx')
2. docx2pdf模块
word转换为pdf
from docx2pdf import convert
import pdfkit
import os
currentDir = os.getcwd()
convert(input_path = currentDir + r"\test.docx", output_path = currentDir + r"\test.pdf")
第二种Word转换为pdf的方法
from win32com.client import constants, gencache
import os
def createPdf(wordPath, pdfPath):
word = gencache.EnsureDispatch("Word.Application")
doc = word.Documents.Open(wordPath, ReadOnly=1)
doc.ExportAsFixedFormat(pdfPath, constants.wdExportFormatPDF)
word.Quit()
wordfiles = []
for file in os.listdir('.'):
if file.endswith(('.doc','.docx')):
wordfiles.append(file)
for file in wordfiles:
filepath = os.path.abspath(file)
index = filepath.rindex('.')
pdfpath = filepath[:index]+'.pdf'
createPdf(filepath, pdfpath)
3. python-pptx模块
- 针对批量PPT的创建和修改i、大量图片、文字的写入、准确无误的插入图标等数据
- python-pptx是第三方模块、自动生成和更新PowerPoint(.pptx)文件
import pptx
from pptx.util import Inches, Pt
from pptx.enum.shapes import MSO_SHAPE
from pptx.dml.color import RGBColor
from pptx.chart.data import CategoryChartData
from pptx.enum.chart import XL_CHART_TYPE
from pptx.enum.chart import XL_LEGEND_POSITION
prs = pptx.Presentation()
slide = prs.slides.add_slide(prs.slide_layouts[0])
prs.slides.add_slide(prs.slide_layouts[1])
prs.slides.add_slide(prs.slide_layouts[2])
print(len(prs.slides))
del prs.slides._sldIdLst[1]
print(len(prs.slides))
text1 = slide.shapes.add_textbox(Inches(5),Inches(5),Inches(5),Inches(5))
text1.text = 'i am yinlei'
p1 = text1.text_frame.add_paragraph()
p1.text = '我是段落1'
p1.add_run().text = '结束'
title_shape = slide.shapes.title
title_shape.text = 'title one'
slide.shapes.placeholders[1].text = 'title two'
shape = slide.shapes.add_shape(MSO_SHAPE.RECTANGLE,Inches(2),Inches(2),Inches(5),Inches(3))
fill = shape.fill
fill.solid()
fill.fore_color.rgb=RGBColor(255,0,0)
line = shape.line
line.color.rgb = RGBColor(55,3,5)
line.width = Pt(2)
table = slide.shapes.add_table(3,3,Inches(2),Inches(2),Inches(4),Inches(2)).table
table.cell(1,0).text = 'name'
table.cell(1,1).text = 'age'
table.cell(1,2).text = 'class'
table.cell(2,0).text = 'yinlei'
table.cell(2,1).text = '21'
table.cell(2,2).text = '1班'
cell = table.cell(0,0)
cell1 = table.cell(0,2)
cell.merge(cell1)
table.cell(0,0).text='student info'
chart_data = CategoryChartData()
chart_data.categories = ['月份','一月份','二月份']
chart_data.add_series('2020', (300,400,500))
chart_data.add_series('2019', (500,300,200))
chart = slide.shapes.add_chart(XL_CHART_TYPE.LINE, Inches(2),Inches(2),Inches(6),Inches(4),chart_data).chart
chart.has_title = True
chart.chart_title.text_frame.text = '销售额'
chart.has_legend = True
chart.legend.position = XL_LEGEND_POSITION.RIGHT
prs.save('yinlei.pptx')
|