The title is right, is twenty-second not 21, do not go back to turn, no leak, the twentieth is to crawl all the data to the Gank.io interface to MySQL, twenty-first has set a good use of Flask to write a API interface, the code is implemented, the deployment of some problems, so stillNo, take a pit first.
This is one of the things in the last two days, because the group is doing something about early teaching in the group. The teacher is going to do a lot of courseware, but most of the content is repeated. Maybe the picture will change, the process may change a little. Then, find me, let me think of the way to myself.Dynamic generation reduces their workload. What they want is to generate PPT automatically.
Do you remember the 5.15 hammer nest conference? Don’t remember? See this picture you should think of what ~
Lao Luo shows the next era of computers: TNT workstation. Here, we will not know all kinds of small mistakes and understanding. The video of the live review can be seen at the B station.
This way of generating simple PPT in batches, I think the idea is nothing more than this:
According to the situation, we define several sets of templates, and then agree on a rule to invoke different templates based on different contents to fill the content.
The next way is to see if Python has a support library to find two pptx and win32com, this section uses the former only, because the latter’s document is really seeing the scalp numbness, and the online examples are very few…
PythonThe commonly used tools for creating and managing virtual environments are: virtualenv and pyvenv, and Pycharm with virtualenv. When new, you can see how to customize, self Baidu, and download modules are all in * *Lib/site-packages * * * directory!
2.Implementation process analysis
First of all, think about what templates are listed below.
- 1.There is only one picture
- 2.There is only one word
- 3.A text and a picture
- 4.Two words
- 5.Four words
And then the layout is about this:
Then a rule is defined, how the data is passed, here is the simplest way to write a TXT file, each line represents a PPT, and the parameters are spaced through a comma, so the complete PPT corresponds to such a TXT file:
By dividing the parameters by comma, we can judge whether there are pictures first. If we take the template 1, 3, we can judge otherwise. OK, the train of thought is coming. Next, let’s start one step at a time.
PS：Here is not to introduce how to use, do not understand themselves to turn the document, I also groped for myself, I have written examples of reference, should feel very gratified.
1.A method of defining a centimetre to an inch
It is thought that the units used in the location and size of PPT are centimeters and need to be converted.
# Centimetre turn in.Def cm_to_in (CM):Return Inches (cm / 2.54)
2.Write a template
The first is the template 1, which is introduced into the object of Presentation, which you can understand as the PPT object, the **.slides.add_slide () method that calls the object to add a slide, and the pptx library provides us with eight different templates,Like you can try one by one, here we use seventh blank slides directly, and the subscript starts from 0, so it is * * * prs.slide_layouts. After the slide is added, Presentation’s save (PPT) is called.File name) * * * * function opens the generated PPT, and then click Set -> the slide size -> select 16:9 directly or set the slide size, for example, the width and height of my slide is the size of our slide, and the rear filled full screen will be used.To this.
Then you call the add_picture function to add a full screen picture.
slide.shapes.add_picture(ppt_bg_path, cm_to_in(0), cm_to_in(0), cm_to_in(25.4), cm_to_in(14.288))
Then the template 1 is finished, and the code is done as follows:
# Template 1: only one pictureDef model_1 (PRS, pic_path):Slide = prs.slides.add_slide (prs.slide_layouts)Slide.shApes.add_picture (pic_path, cm_to_in (0), cm_to_in (0), cm_to_in (25.4), cm_to_in (14.288))Call:PreseNtation = Presentation (ppt_file_name)Model_1 (presentation, laoluo_bg_path)
Open the generated ppt:
Oh, the successful generation, then to the template 2:
Fill in the full screen image and create a new text box:
title_box = slide.shapes.add_textbox(cm_to_in(3.89), cm_to_in(5.35), cm_to_in(17.61), cm_to_in(3.59))
Then add a text field:
paragraph = title_box.text_frame.add_paragraph()
Then text related operations in the text domain can be done:
paragraph.text = title # Set up textParagraph.vertical_anchor = MSO_VERTICAL_ANCHOR.MIDDLE, set vertical alignment.Paragraph.alignment = PP_PARAGRAPH_ALIGNMENT.CENTER horizontal alignmentParagraph.font.size = Pt (60), set the text size, PT stands for pound.Paragraph.font.name = 'Microsoft Black' set fontsParagraph.font.color.rgb = RGBColor (255, 255, 255)
Under parameter passing, call this template 2 to see the effect generated:
The following three templates are followed. The main difficulty is the location and width setting of the control. There are two operations:
The simplest operation:
Just fill in a coordinate and width height, open the generated PPT after running, adjust the position by yourself, then record the location and width of the location, then change the code.
The operation of complex points:
Self calculation, for example, template 5, three subheadings, first minus the distance between the left and right, then three equal points, and dynamically calculate the initial position of the subtitle.
3.Configuration file read
Next, write a function to read the contents of the file and call the corresponding method. The code is as follows
# The method of reading the configuration file to call the templateDef read_rules (PRS, filename):If os.path.exists (filename):With open (filename,'r+')Encoding='utf-8') as f:For rule in f:Word_list = rule.replace ('\n',' ').Split (', ')If'png'IN rule or'jpg'in rule:If len (word_list) = = 1:Model_1 (PRS, os.path.join (c.res_pictures, word_li)St))Else:Model_3 (PRS, word_list, os.path.join (c.res_pictures, word_list))Else:If leN (word_list) = = 1:Model_2 (PRS, word_list)Elif len (word_list) = = 2:Model_4 (PRS, word_list)Word_list)Elif len (word_list) = = 4:Model_5 (PRS, word_list, word_list, word_list, w)Ord_list)
Call the function read by the configuration file
if __name__ == '__main__': t.is_dir_existed(c.outputs_documents_path) ppt_existed(ppt_file_name) presentation = Presentation(ppt_file_name) read_rules(presentation, rules_path) presentation.save(ppt_file_name)
4.There are some things to say
Batch production is quite cool, but it does not support direct animation. And then this simple PPT, actually, the efficiency of copying and modifying the picture may be faster than one of your templates… I have to say some chicken ribs, oh, yes, this also supports the generation of a chart, how to do it all.The official documents can be consulted on their own.
The other win32com library, which seems to be more powerful, may support animation, but the document is really difficult to gnawing. It doesn’t go deep into the study and is interested in making a toss. Attached are the solutions to the problems encountered in using win32com library.
- 1.Python3Install win32com
pip install pypiwin32
- 2.Where has the win32com document
Microsoft official network has, but see the brain pain, introduce a tool: OLEVIEW, a pile of online, but the use of this will meet a problem, here with the record of the author win10 computer encountered some of the situation:
Search the DLL file on the Internet, copy the file to the Windows/system32, and then open the command prompt, and type the Regsvr32 iviewers.dll registry to register the DLL runtime. NoAfter that, win10 64 bits will be mistaken:
Module iviewers.dll may be incompatible with the Windows version you are running to check if the module is compatible with the x86 or x64 version of regsvr.exe.
What you want to do is to copy the DLL file to the Windows/SysWOW64, and then the administrator mode opens the command prompt CD to the directory and then executes the Regsvr32 iviewers.dll.
If there is a failure of the call to the DllRegisterServer, the error code is 0x80070005, and that is UAC, and you don’t open the command prompt with the administrator mode.
If it is solved, you should be able to open it normally, call the PowerPoint on the left, double click open, and then, this tool does not support the lookup, and you can select the content and copy it to a code view tool such as Sublime Text, Ctrl + F lookupKeyword, and then step by step to locate some functions needed to achieve a function.
This section explains the use of the Python-pptx wave volume generated N PPT routines, can not help but sigh again and I Python, finally I wish happy Children’s Day ~
- python-pptxLibrary Chinese documents and use examples
- pythonThe experience of using win32com
Appendix: the final code is available at: github.com/coder-pig/R. To find):
import pptx import config as c import tools as t from pptx import Presentation from pptx.dml.color import RGBColor from pptx.util import Inches, Pt from pptx.enum.text import MSO_VERTICAL_ANCHOR, PP_PARAGRAPH_ALIGNMENT import os rules_path = os.path.join(c.res_documents, 'ppt_rules.txt') ppt_bg_path = os.path.join(c.res_pictures, 'ppt_bg.png') laoluo_bg_path = os.path.join(c.res_pictures, 'laoluo.jpg') last_bg_path = os.path.join(c.res_pictures, 'last.png') story_bg_path = os.path.join(c.res_pictures, 'story.png') ppt_file_name = os.path.join(c.outputs_documents_path, 'result.pptx') # Centimetre turn in.Def cm_to_in (CM):Return Inches (cm / 2.54)A new blank for judging whether courseware exists or not.Def ppt_existed (ppt_name):If not os.path.exists (ppt_name):PRS = Presentation ()Prs.slide_height = cm_to_in (14.35)Prs.slidE_width = cm_to_in (25.5)Prs.save (ppt_name)Template 1: only one pictureDef model_1 (PRS, pic_path):Slide = prs.sLides.add_slide (prs.slide_layouts)Slide.shapes.add_picture (pic_path, cm_to_in (0), cm_to_in (0),Cm_to_in (25.4), cm_to_in (14.288))Template 2: only one titleDef model_2 (PRS, title):Slide = prs.slides.add_slidE (prs.slide_layouts)Slide.shapes.add_picture (ppt_bg_path, cm_to_in (0), cm_to_in (0), cm_to_in (25)..4), cm_to_in (14.288))Title_box = slide.shapes.add_textbox (cm_to_in (3.89), cm_to_in (5.35), cm_to_i).N (17.61), cm_to_in (3.59))Paragraph = title_box.text_frame.add_paragraph ()Paragraph.text = titleParagraph.vertical_anchor = MSO_VERTICAL_ANCHOR.MIDDLEParagraph.alignment = PP_PARAGRAPH_ALIGNMENT.CENTERParagraph.font.size = Pt (60)Paragraph.font.name = 'Microsoft Ya Black'Paragraph.font.color.rgb = RGBColor (255, 255, 255)Template 3: there is a word, there is a pictureDef model_3 (PRS, title, pic_path):Slide = prs.slides.add_slide (PRs.slide_layouts)Slide.shapes.add_picture (ppt_bg_path, cm_to_in (0), cm_to_in (0), cm_to_in (25.4))Cm_to_in (14.288))IMG = slide.shapes.add_picture (pic_path, cm_to_in (0), cm_to_in (0), height=cm_to_in (11.72))Img.left = int (prs.slide_width / 2 + (prs.slide_width / 2 - img.width) / 2)Img.top =Int (((prs.slide_height - img.height) / 2)Title_box = slide.shapes.add_textbox (cm_to_in (2), cm_to_in(5.35), int (prs.slide_width / 3), cm_to_in (3.59))Paragraph = title_box.text_frame.add_paragraph ()Paragraph.text = titleParagraph.font.size = Pt (44)Paragraph.font.name = 'Microsoft Ya Black'Paragraph.font.Color.rgb = RGBColor (255, 255, 255)4: two lines of text, one big and one smallDef model_4 (PRS, title, content):Slide = prs.sLides.add_slide (prs.slide_layouts)Slide.shapes.add_picture (ppt_bg_path, cm_to_in (0), cm_to_in (0)), cm_to_in (25.4), cm_to_in (14.288))First class titleTitle_box_1 = slide.shapes.add_textbox (cm_to_in (1.27)).Cm_to_in (2.04), cm_to_in (22.86), cm_to_in (3.18))Paragraph_1 = title_box_1.text_frame.add_paragrapH ()Paragraph_1.text = titleParagraph_1.vertical_anchor = MSO_VERTICAL_ANCHOR.MIDDLEParagraph_1.alignment = PP_PARAGRAPH_ALIGNMENT.CENTERParagraph_1.font.size = Pt (44)Paragraph_1.font.name ='Microsoft Ya Black'Paragraph_1.font.color.rgb = RGBColor (255, 255, 255)Two level headlinesTitle_box_2 = slide.shapes.add_textbox (cm_to_in (7.46), cm_to_in (6.4), cm_to_in (10.47), cm_to_in (2.39))Paragraph_2 = title_box_2.Text_frame.add_paragraph ()Paragraph_2.text = titleParagraph_2.vertical_anchor = MSO_VERTICAL_ANCHOR.MIDDLEParagraph_2.alignment = PP_PARAGRAPH_ALIGNMENT.CENTERParagraph_2.font.size = Pt (32)PAragraph_2.font.name = 'Microsoft Ya Black'Paragraph_2.font.color.rgb = RGBColor (255, 255, 255)Template 5: one line text, multiple small headlinesDef model_5 (PRS, title, *content):Slide = prs.slides.add_slide (prs.slide_layouts)Slide.shapeS.add_picture (ppt_bg_path, cm_to_in (0), cm_to_in (0), cm_to_in (25.4), cm_to_in (14.288))Title_box_1= slide.shapes.add_textbox (cm_to_in (1.27), cm_to_in (2.04), cm_to_in (22.86), cm_to_in (3.18))ParagraPh_1 = title_box_1.text_frame.add_paragraph ()Paragraph_1.text = titleParagraph_1.vertical_anchor= MSO_VERTICAL_ANCHOR.MIDDLEParagraph_1.alignment = PP_PARAGRAPH_ALIGNMENT.CENTERParagraph_1.foNt.size = Pt (44)Paragraph_1.font.name = 'Microsoft Ya Black'Paragraph_1.font.color.rgb = RGBColor (255, 255, 25)5)Dynamic construction of small headlinesModule_width = (prs.slide_width - cm_to_in (1.27) * 2) / len (content)For I in range (0, 3):Title_box = slide.shapes.add_textbox (cm_to_in (1.27) + I * module_width, cm_to_in (6.4), modulE_width,Cm_to_in (2.39))Paragraph = title_box.text_frame.add_paragraph ()Paragraph.text = contenT[i]Paragraph.vertical_anchor = MSO_VERTICAL_ANCHOR.MIDDLEParagraph.alignment = PP_PARAGRAPH_ALIGNMENT.CENTERParagraph.font.size = Pt (32)Paragraph.font.name = 'Microsoft Ya Black'Paragraph.font.color.rgb= RGBColor (255, 255, 255)The method of reading the configuration file to call the templateDef read_rules (PRS, filename):If os.path.exists (filename):With open (filename,'r+', encoding='utf-8') as f:For rule in f:Word_list = rule.replace ('\n'.split (',')If'png'in rule or'jpg' in rule:If len (word_list) = = 1:Model_1 (PRS, os.path.)Join (c.res_pictures, word_list))Else:Model_3 (PRS, word_list, os.path.join (c.res_pictures).Word_list))Else:If len (word_list) = = 1:Model_2 (PRS, word_list)Elif len (word_list) = = 2:Model_4 (PRS, word_list, word_list)Elif len (word_list) = = 4:Model_5 (PRS, word_list, w)Ord_list, word_list, word_list)If __name__ = ='__main__':T.is_dir_existed (c.outputs_docuMents_path)Ppt_existed (ppt_file_name)Presentation = Presentation (ppt_file_name)Read_rules (PRESEntation, rules_path)Presentation.save (ppt_file_name)
Welcome to my blog or public address: https://home.cnblogs.com/u/Python1234/ Python learning and communication.
Welcome to join my thousand people learning group: 125240963
- (3) Microsoft office Word 2013 version entry entry paragraph setting
- (3) Microsoft office Word 2013 version entry entry paragraph setting
- How do you implement the following layout without flex?
- The first day of HTML
- The first day of HTML
- Word cloud drawing wordcloud
- Word cloud drawing wordcloud
- HTML day 4 record
- HTML day 4 record
- .net uses AsposeWord to export the word table table