日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

用Python自动化操作PPT,看完这篇文章就够了!

發(fā)布時(shí)間:2023/12/20 python 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 用Python自动化操作PPT,看完这篇文章就够了! 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

作者:超級(jí)大洋蔥806

https://tangxing.blog.csdn.net/article/details/109568830


大家好,我是小z

今天給大家分享一波Python自動(dòng)化操作PPT的干貨

1.PPT自動(dòng)化能干什么?有什么優(yōu)勢(shì)?

  • 它可以代替你自動(dòng)制作PPT

  • 它可以減少你調(diào)整用于調(diào)整PPT格式的時(shí)間

  • 它可以讓數(shù)據(jù)報(bào)告風(fēng)格一致

  • 總之就是:它能提高你的工作效率!讓你有更多時(shí)間去做其他事情!

2.使用win32com操作ppt

官方文檔:https://docs.microsoft.com/zh-cn/office/vba/api/powerpoint.shape.copy

2.1 pip安裝win32com

pip?install?pypiwin32

由于我已經(jīng)安裝過了,這里提示已經(jīng)安裝

2.2 win32com復(fù)制ppt模板

有時(shí)候我們需要對(duì)ppt的模板進(jìn)行復(fù)制,然后再添加相應(yīng)內(nèi)容,由于python-pptx對(duì)復(fù)制模板也沒有很好的支持(我沒找到~憂傷),所以我們用win32com對(duì)模板頁(yè)進(jìn)行復(fù)制,然后再用python-pptx增加ppt內(nèi)容。

參考官方文檔:https://docs.microsoft.com/zh-cn/office/vba/api/powerpoint.slide.copy

先準(zhǔn)備好一張模板: 2.2 win32 ppt測(cè)試.pptx

示例代碼:

import?win32com from?win32com.client?import?Dispatch import?osppt?=?Dispatch('PowerPoint.Application') #?或者使用下面的方法,使用啟動(dòng)獨(dú)立的進(jìn)程: #?ppt?=?DispatchEx('PowerPoint.Application')#?如果不聲明以下屬性,運(yùn)行的時(shí)候會(huì)顯示的打開word ppt.Visible?=?1??#?后臺(tái)運(yùn)行 ppt.DisplayAlerts?=?0??#?不顯示,不警告#?創(chuàng)建新的PowerPoint文檔 #?pptSel?=?ppt.Presentations.Add()? #?打開一個(gè)已有的PowerPoint文檔 pptSel?=?ppt.Presentations.Open(os.getcwd()?+?"\\"?+?"2.2?win32?ppt測(cè)試.pptx")#?復(fù)制模板頁(yè) pptSel.Slides(1).Copy() #設(shè)置需要復(fù)制的模板頁(yè)數(shù) pageNums?=?10 #?粘貼模板頁(yè) for?i?in?range(pageNums):pptSel.Slides.Paste()#?pptSel.Save()??#?保存 pptSel.SaveAs(os.getcwd()?+?"\\"?+?"win32_copy模板.pptx")??#?另存為 pptSel.Close()??#?關(guān)閉?PowerPoint?文檔 ppt.Quit()??#?關(guān)閉?office

效果如下:

3.python-pptx 創(chuàng)建PPT、復(fù)制頁(yè)面

官方文檔:https://python-pptx.readthedocs.io/en/latest/

3.1 pip安裝python-pptx

安裝方法:

pip?install?python-pptx

我已經(jīng)安裝過了,故提示已經(jīng)安裝

3.2 python-pptx 復(fù)制頁(yè)面

使用python-pptx進(jìn)行復(fù)制沒有找到合適的方法,有以下兩種解決辦法:

  • 使用win32com對(duì)ppt模板進(jìn)行復(fù)制

  • 增加模板ppt數(shù)量,然后使用python-pptx對(duì)不需要的模板頁(yè)進(jìn)行刪減操作

  • 3.3 python-pptx 刪除頁(yè)面

    python-pptx 多頁(yè)待刪除模板.pptx:

    示例代碼:

    from?pptx?import?Presentation#?刪除某一頁(yè)ppt def?del_slide(prs,index):slides?=?list(prs.slides._sldIdLst)prs.slides._sldIdLst.remove(slides[index])#?3.3?python-pptx?刪除頁(yè)面 def?fun3_3():#?打開pptppt?=?Presentation('python-pptx?多頁(yè)待刪除模板.pptx')#?獲取所有頁(yè)slides?=?ppt.slidesnumber_pages?=?len(slides)print("刪除前ppt一共",number_pages,"頁(yè)面")#?設(shè)置需要?jiǎng)h除的頁(yè)面數(shù)量delPageNums?=?3#?進(jìn)行刪除操作(每次都刪除第一張ppt)for?index?in?range(delPageNums):del_slide(ppt,0)#?再次獲取所有頁(yè)slides?=?ppt.slidesnumber_pages?=?len(slides)print("刪除后ppt一共",number_pages,"頁(yè)面")ppt.save('python-pptx?多頁(yè)已刪除模板.pptx')print('生成完畢')if?__name__?==?'__main__':fun3_3()

    執(zhí)行效果:

    3.4 新建頁(yè)面

    示例代碼:

    from?pptx?import?Presentation#?新建ppt ppt?=?Presentation()#?新建頁(yè)面 slide?=?ppt.slides.add_slide(ppt.slide_layouts[0])#?保存ppt ppt.save('新建ppt.pptx')

    效果如下:

    4.python-pptx 插入文字、表格、形狀并設(shè)置樣式

    模板ppt:

    接下來,我們就在此模板上進(jìn)行我們的操作演示

    4.1 python-pptx 添加文字并設(shè)置樣式

    4.1.1 添加單行文字與多行文字

    示例代碼:

    from?pptx?import?Presentation from?pptx.util?import?Pt,Cm#?打開已存在ppt ppt?=?Presentation('4.?python-pptx操作模板.pptx')#?設(shè)置添加到當(dāng)前ppt哪一頁(yè) n_page?=?0 singleLineContent?=?"我是單行內(nèi)容" multiLineContent?=?\ """我是多行內(nèi)容1 我是多行內(nèi)容2 我是多行內(nèi)容3 """#?獲取需要添加文字的頁(yè)面對(duì)象 slide?=?ppt.slides[n_page]#?添加單行內(nèi)容#?設(shè)置添加文字框的位置以及大小 left,?top,?width,?height?=?Cm(16.9),?Cm(1),?Cm(12),?Cm(1.2) #?添加文字段落 new_paragraph1?=?slide.shapes.add_textbox(left=left,?top=top,?width=width,?height=height).text_frame #?設(shè)置段落內(nèi)容 new_paragraph1.paragraphs[0].text?=?singleLineContent #?設(shè)置文字大小 new_paragraph1.paragraphs[0].font.size?=?Pt(15)#?添加多行#?設(shè)置添加文字框的位置以及大小 left,?top,?width,?height?=?Cm(16.9),?Cm(3),?Cm(12),?Cm(3.6) #?添加文字段落 new_paragraph2?=?slide.shapes.add_textbox(left=left,?top=top,?width=width,?height=height).text_frame #?設(shè)置段落內(nèi)容 new_paragraph2.paragraphs[0].text?=?multiLineContent #?設(shè)置文字大小 new_paragraph2.paragraphs[0].font.size?=?Pt(15)#?保存ppt ppt.save('4.1?添加文字.pptx')

    效果如下:

    4.1.2 設(shè)置文字框樣式與文字樣式

    示例代碼:

    from?pptx?import?Presentation from?pptx.util?import?Pt,Cm from?pptx.dml.color?import?RGBColor from?pptx.enum.text?import?MSO_VERTICAL_ANCHOR,?PP_PARAGRAPH_ALIGNMENT from?pptx.enum.text?import?PP_ALIGN#?打開已存在ppt ppt?=?Presentation('4.?python-pptx操作模板.pptx')#?獲取需要添加文字的頁(yè)面對(duì)象 slide?=?ppt.slides[0]#?設(shè)置添加文字框的位置以及大小 left,?top,?width,?height?=?Cm(16.9),?Cm(1),?Cm(12),?Cm(1.2) #?添加文字框?slide.shapes.add_textbox(距離左邊,距離頂端,寬度,高度) textBox?=?slide.shapes.add_textbox(left=left,?top=top,?width=width,?height=height)#?調(diào)整文本框背景顏色 textBoxFill?=?textBox.fill textBoxFill.solid()??#?純色填充 textBoxFill.fore_color.rgb?=?RGBColor(187,?255,?255)#?文本框邊框樣式調(diào)整 line?=?textBox.line line.color.rgb?=?RGBColor(0,?255,?0) line.width?=?Cm(0.1)#?獲取文本框?qū)ο?tf?=?textBox.text_frame#?文本框樣式調(diào)整 tf.margin_bottom?=?Cm(0.1)??#?下邊距 tf.margin_left?=?0??#?左邊距 tf.vertical_anchor?=?MSO_VERTICAL_ANCHOR.BOTTOM??#?對(duì)齊文本方式:底端對(duì)齊 tf.word_wrap?=?True??#?文本框的文字自動(dòng)對(duì)齊#?設(shè)置內(nèi)容 tf.paragraphs[0].text?=?'這是一段文本框里的文字'#?字體樣式調(diào)整 tf.paragraphs[0].alignment?=?PP_ALIGN.CENTER??#?對(duì)齊方式 tf.paragraphs[0].font.name?=?'微軟雅黑'??#?字體名稱 tf.paragraphs[0].font.bold?=?True??#?是否加粗 tf.paragraphs[0].font.italic?=?True??#?是否斜體 tf.paragraphs[0].font.color.rgb?=?RGBColor(255,?0,?0)??#?字體顏色 tf.paragraphs[0].font.size?=?Pt(20)??#?字體大小#?保存ppt ppt.save('4.1.2?設(shè)置文字框與字體樣式.pptx')

    效果如下:

    代碼詳解

    • 添加文本框

      #?添加文字框?slide.shapes.add_textbox(距離左邊,距離頂端,寬度,高度) textBox?=?slide.shapes.add_textbox(left=left,?top=top,?width=width,?height=height)
    • 設(shè)置文本框背景

      #?調(diào)整文本框背景顏色 textBoxFill?=?textBox.fill textBoxFill.solid()??#?純色填充 textBoxFill.fore_color.rgb?=?RGBColor(187,?255,?255)

      RGB顏色參考:http://www.wahart.com.hk/rgb.htm

    • 設(shè)置文本框邊框樣式

      #?文本框邊框樣式調(diào)整 line?=?textBox.line line.color.rgb?=?RGBColor(0,?255,?0) line.width?=?Cm(0.1)
    • 設(shè)置文本框文字樣式

      #?獲取文本框文字對(duì)象 tf?=?textBox.text_frame#?文本框樣式調(diào)整 tf.margin_bottom?=?Cm(0.1)??#?下邊距 tf.margin_left?=?0??#?左邊距 tf.vertical_anchor?=?MSO_VERTICAL_ANCHOR.BOTTOM??#?垂直方式:底端對(duì)齊 tf.word_wrap?=?True??#?文本框的文字自動(dòng)對(duì)齊 “

      指定文本在文本框架中的垂直對(duì)齊方式。與TextFrame對(duì)象的.vertical_anchor屬性一起使用。請(qǐng)注意,vertical_anchor屬性也可以具有值None,表示沒有直接指定的垂直錨設(shè)置,并且其有效值是從占位符繼承的(如果有一個(gè)或從主題繼承)。也可以不指定任何內(nèi)容來刪除明確指定的垂直錨設(shè)置。

      from?pptx.enum.text?import?MSO_ANCHORcell?=?table.cell(row_idx=2,?col_idx=3) cell.vertical_anchor?=?MSO_ANCHOR.BOTTOM TOPAligns text to top of text frame and inherits its value from its layout placeholder or theme. MIDDLECenters text vertically BOTTOMAligns text to bottom of text frame MIXEDReturn value only; indicates a combination of the other states.
      • 垂直對(duì)齊

    • 設(shè)置文本框內(nèi)容

      #?設(shè)置內(nèi)容 tf.paragraphs[0].text?=?'這是一段文本框里的文字'
    • 字體樣式調(diào)整

      #?字體樣式調(diào)整 tf.paragraphs[0].alignment?=?PP_ALIGN.CENTER??#?對(duì)齊方式 tf.paragraphs[0].font.name?=?'微軟雅黑'??#?字體名稱 tf.paragraphs[0].font.bold?=?True??#?是否加粗 tf.paragraphs[0].font.italic?=?True??#?是否斜體 tf.paragraphs[0].font.color.rgb?=?RGBColor(255,?0,?0)??#?字體顏色 tf.paragraphs[0].font.size?=?Pt(20)??#?字體大小 from?pptx.enum.text?import?PP_ALIGNshape.paragraphs[0].alignment?=?PP_ALIGN.CENTER
      • 文字對(duì)齊

    CENTERCenter alignDISTRIBUTEEvenly distributes e.g. Japanese characters from left to right within a lineJUSTIFYJustified, i.e. each line both begins and ends at the margin with spacing between words adjusted such that the line exactly fills the width of the paragraph.JUSTIFY_LOWJustify using a small amount of space between words.LEFTLeft alignedRIGHTRight alignedTHAI_DISTRIBUTEThai distributedMIXEDReturn value only; indicates multiple paragraph alignments are present in a set of paragraphs.
    • 保存ppt

    ??#?保存pptppt.save('4.1.2?設(shè)置文字框與字體樣式.pptx')

    4.2 python-pptx 添加表格并設(shè)置樣式

    示例代碼:

    from?pptx?import?Presentation from?pptx.util?import?Pt,Cm from?pptx.dml.color?import?RGBColor from?pptx.enum.text?import?MSO_ANCHOR from?pptx.enum.text?import?PP_ALIGN#?設(shè)置需要添加到哪一頁(yè) n_page?=?0#?打開已存在ppt ppt?=?Presentation('4.?python-pptx操作模板.pptx')#?獲取slide對(duì)象 slide?=?ppt.slides[n_page]#?設(shè)置表格位置和大小 left,?top,?width,?height?=?Cm(6),?Cm(12),?Cm(13.6),?Cm(5) #?表格行列數(shù),和大小 shape?=?slide.shapes.add_table(6,?7,?left,?top,?width,?height) #?獲取table對(duì)象 table?=?shape.table#?設(shè)置列寬 table.columns[0].width?=?Cm(3) table.columns[1].width?=?Cm(2.3) table.columns[2].width?=?Cm(2.3) table.columns[3].width?=?Cm(1.3) table.columns[4].width?=?Cm(1.3) table.columns[5].width?=?Cm(1.3) table.columns[6].width?=?Cm(2.1)#?設(shè)置行高 table.rows[0].height?=?Cm(1)#?合并首行 table.cell(0,?0).merge(table.cell(0,?6))#?填寫標(biāo)題 table.cell(1,?0).text?=?"時(shí)間" table.cell(1,?1).text?=?"階段" table.cell(1,?2).text?=?"執(zhí)行用例" table.cell(1,?3).text?=?"新增問題" table.cell(1,?4).text?=?"問題總數(shù)" table.cell(1,?5).text?=?"遺留問題" table.cell(1,?6).text?=?"遺留致命/"?\"嚴(yán)重問題"#?填寫變量?jī)?nèi)容 table.cell(0,?0).text?=?"產(chǎn)品1" content_arr?=?[["4/30-5/14",?"DVT1",?"20",?"12",?"22",?"25",?"5"],["5/15-5/21",?"DVT1",?"25",?"32",?"42",?"30",?"8"],["5/22-6/28",?"DVT1",?"1",?"27",?"37",?"56",?"12"],["5/22-6/28",?"DVT1",?"1",?"27",?"37",?"56",?"12"]]#?修改表格樣式 for?rows?in?range(6):for?cols?in?range(7):#?Write?column?titlesif?rows?==?0:#?設(shè)置文字大小table.cell(rows,?cols).text_frame.paragraphs[0].font.size?=?Pt(15)#?設(shè)置字體table.cell(rows,?cols).text_frame.paragraphs[0].font.name?=?'微軟雅黑'#?設(shè)置文字顏色table.cell(rows,?cols).text_frame.paragraphs[0].font.color.rgb?=?RGBColor(255,?255,?255)#?設(shè)置文字左右對(duì)齊table.cell(rows,?cols).text_frame.paragraphs[0].alignment?=?PP_ALIGN.CENTER#?設(shè)置文字上下對(duì)齊table.cell(rows,?cols).vertical_anchor?=?MSO_ANCHOR.MIDDLE#?設(shè)置背景為填充table.cell(rows,?cols).fill.solid()#?設(shè)置背景顏色table.cell(rows,?cols).fill.fore_color.rgb?=?RGBColor(34,?134,?165)elif?rows?==?1:table.cell(rows,?cols).text_frame.paragraphs[0].font.size?=?Pt(10)table.cell(rows,?cols).text_frame.paragraphs[0].font.name?=?'微軟雅黑'??#?字體名稱table.cell(rows,?cols).text_frame.paragraphs[0].font.color.rgb?=?RGBColor(0,?0,?0)table.cell(rows,?cols).text_frame.paragraphs[0].alignment?=?PP_ALIGN.CENTERtable.cell(rows,?cols).vertical_anchor?=?MSO_ANCHOR.MIDDLEtable.cell(rows,?cols).fill.solid()table.cell(rows,?cols).fill.fore_color.rgb?=?RGBColor(204,?217,?225)else:table.cell(rows,?cols).text?=?content_arr[rows?-?2][cols]table.cell(rows,?cols).text_frame.paragraphs[0].font.size?=?Pt(10)table.cell(rows,?cols).text_frame.paragraphs[0].font.name?=?'微軟雅黑'??#?字體名稱table.cell(rows,?cols).text_frame.paragraphs[0].font.color.rgb?=?RGBColor(0,?0,?0)table.cell(rows,?cols).text_frame.paragraphs[0].alignment?=?PP_ALIGN.CENTERtable.cell(rows,?cols).vertical_anchor?=?MSO_ANCHOR.MIDDLEtable.cell(rows,?cols).fill.solid()table.cell(rows,?cols).fill.fore_color.rgb?=?RGBColor(204,?217,?225)ppt.save('4.2?python-pptx?添加表格并設(shè)置樣式.pptx')

    效果如下:

    4.3 python-pptx 添加圖表并設(shè)置樣式

    示例代碼:

    from?pptx?import?Presentation from?pptx.util?import?Pt,Cm from?pptx.chart.data?import?ChartData from?pptx.enum.chart?import?XL_CHART_TYPE#?設(shè)置需要添加到哪一頁(yè) n_page?=?0#?打開已存在ppt ppt?=?Presentation('4.?python-pptx操作模板.pptx')#?獲取slide對(duì)象 slide?=?ppt.slides[n_page]#?初始化圖表 chart_data?=?ChartData()#?填充需要添加的內(nèi)容 content_arr?=?[["4/30-5/14",?"DVT1",?"20",?"12",?"22",?"25",?"5"],["5/15-5/21",?"DVT1",?"25",?"32",?"42",?"30",?"8"],["5/22-6/28",?"DVT1",?"1",?"27",?"37",?"56",?"12"],["5/22-6/28",?"DVT1",?"1",?"27",?"37",?"56",?"12"]]#?填充圖表 chart_data.categories?=?[content_arr[0][0],?content_arr[1][0],?content_arr[2][0],?content_arr[3][0]] chart_data.add_series("問題總數(shù)",?(content_arr[0][4],?content_arr[1][4],?content_arr[2][4],?content_arr[3][4])) chart_data.add_series("遺留問題總數(shù)",?(content_arr[0][5],?content_arr[1][5],?content_arr[2][5],?content_arr[3][5])) chart_data.add_series("遺留致命嚴(yán)重\n問題總數(shù)",?(content_arr[0][6],?content_arr[1][6],?content_arr[2][6],?content_arr[3][6]))#?設(shè)置位置 left,?top,?width,?height?=?Cm(6),?Cm(10),?Cm(16.1),?Cm(7.5) #?添加圖表 chart?=?slide.shapes.add_chart(XL_CHART_TYPE.LINE,?left,?top,?width,?height,?chart_data ).chartchart.has_legend?=?True chart.legend.include_in_layout?=?False #?chart.series[0].smooth?=?True?#?是否平滑 #?chart.series[1].smooth?=?True #?chart.series[2].smooth?=?True chart.font.size?=?Pt(10)??#?文字大小ppt.save('4.3?python-pptx?添加圖表并設(shè)置樣式.pptx') print('折線圖添加完成')

    效果如下:

    其它圖表可參考:https://www.cnblogs.com/adam012019/p/11348938.html

    4.4 python-pptx 添加形狀并設(shè)置樣式

    這里的形狀可以是這些:

    形狀別名可以再這里查看:

    https://docs.microsoft.com/zh-cn/office/vba/api/Office.MsoAutoShapeType

    并對(duì)應(yīng)這里,找到正確的枚舉名:

    https://python-pptx.readthedocs.io/en/latest/api/enum/MsoAutoShapeType.html#msoautoshapetype

    程序示例:

    from?pptx?import?Presentation from?pptx.util?import?Pt,Cm from?pptx.dml.color?import?RGBColor from?pptx.enum.text?import?PP_ALIGN from?pptx.enum.shapes?import?MSO_SHAPE#?設(shè)置需要添加到哪一頁(yè) n_page?=?0#?打開已存在ppt ppt?=?Presentation('4.?python-pptx操作模板.pptx')#?獲取slide對(duì)象 slide?=?ppt.slides[n_page]#?添加矩形 #?設(shè)置位置以及大小 left,?top,?width,?height?=?Cm(2.5),?Cm(4.5),?Cm(30),?Cm(0.5) #?添加形狀 rectangle?=?slide.shapes.add_shape(MSO_SHAPE.RECTANGLE,?left,?top,?width,?height) #?設(shè)置背景填充 rectangle.fill.solid() #?設(shè)置背景顏色 rectangle.fill.fore_color.rgb?=?RGBColor(34,?134,?165) #?設(shè)置邊框顏色 rectangle.line.color.rgb?=?RGBColor(34,?134,?165)#?添加正三角+文字(正常) left,?top,?width,?height?=?Cm(3),?Cm(5.1),?Cm(0.5),?Cm(0.4) slide.shapes.add_shape(MSO_SHAPE.FLOWCHART_EXTRACT,?left,?top,?width,?height) new_paragraph?=?slide.shapes.add_textbox(left=left?-?Cm(0.95),?top=top?+?Cm(0.4),?width=Cm(2.4),height=Cm(1.1)).text_frame content?=?"""2020/01/05 內(nèi)容1""" new_paragraph.paragraphs[0].text?=?content new_paragraph.paragraphs[0].font.size?=?Pt(10)??#?文字大小 new_paragraph.paragraphs[0].alignment?=?PP_ALIGN.CENTER#?添加正三角+文字(延期) left,?top,?width,?height?=?Cm(9),?Cm(5.1),?Cm(0.5),?Cm(0.4) extract?=?slide.shapes.add_shape(MSO_SHAPE.FLOWCHART_EXTRACT,?left,?top,?width,?height) extract.fill.solid() extract.fill.fore_color.rgb?=?RGBColor(255,?0,?0) extract.line.color.rgb?=?RGBColor(255,?0,?0)new_paragraph?=?slide.shapes.add_textbox(left=left?-?Cm(0.95),?top=top?+?Cm(0.4),?width=Cm(2.4),height=Cm(1.1)).text_frame content?=?"""2020/01/05 內(nèi)容2""" new_paragraph.paragraphs[0].text?=?content??#?文字內(nèi)容 new_paragraph.paragraphs[0].font.size?=?Pt(10)??#?文字大小 new_paragraph.paragraphs[0].font.color.rgb?=?RGBColor(255,?0,?0)????#?文字顏色 new_paragraph.paragraphs[0].alignment?=?PP_ALIGN.CENTER?#?文字水平對(duì)齊方式#?添加倒三角+間隔條+文字 left,?top,?width,?height?=?Cm(5),?Cm(4),?Cm(0.5),?Cm(0.4) slide.shapes.add_shape(MSO_SHAPE.FLOWCHART_MERGE,?left,?top,?width,?height) gap?=?slide.shapes.add_shape(MSO_SHAPE.RECTANGLE,?left?+?Cm(0.2),?top?+?Cm(0.5),?Cm(0.05),?Cm(0.5)) gap.fill.solid() gap.fill.fore_color.rgb?=?RGBColor(255,?255,?255) gap.line.color.rgb?=?RGBColor(255,?255,?255)new_paragraph?=?slide.shapes.add_textbox(left=left?-?Cm(0.95),?top=top?-?Cm(1),?width=Cm(2.4),height=Cm(1.1)).text_frame content?=?"""2020/01/05 內(nèi)容3""" new_paragraph.paragraphs[0].text?=?content new_paragraph.paragraphs[0].font.size?=?Pt(10)??#?文字大小 new_paragraph.paragraphs[0].alignment?=?PP_ALIGN.CENTER#?添加當(dāng)前時(shí)間圖形 left,?top,?width,?height?=?Cm(7),?Cm(4),?Cm(0.5),?Cm(0.4) now?=?slide.shapes.add_shape(MSO_SHAPE.DOWN_ARROW,?left,?top,?width,?height) now.fill.solid() now.fill.fore_color.rgb?=?RGBColor(254,?152,?47) now.line.color.rgb?=?RGBColor(254,?152,?47)ppt.save('4.4?python-pptx?添加形狀并設(shè)置樣式.pptx') print('進(jìn)度條添加完成')

    效果如下:

    5.seaborn繪圖庫(kù)介紹與使用

    官方網(wǎng)址:http://seaborn.pydata.org/

    • seaborn是基于Matplotlib的Python數(shù)據(jù)可視化庫(kù)。它提供了一個(gè)高級(jí)界面,用于繪制引人入勝且內(nèi)容豐富的統(tǒng)計(jì)圖形

    • 只是在Matplotlib上進(jìn)行了更高級(jí)的API封裝,從而使作圖更加容易

    • seaborn是針對(duì)統(tǒng)計(jì)繪圖的,能滿足數(shù)據(jù)分析90%的繪圖需求,需要復(fù)雜的自定義圖形還需要使用到Matplotlib

    5.1 pip安裝seaborn

    pip?install?seaborn

    效果如下(我的顯示已安裝):

    使用:

    import?seaborn?as?sns? #?或者 import?seaborn

    使用數(shù)據(jù)集:

    import?seaborn?as?snstips?=?sns.load_dataset("tips")

    無法連接:

    下載數(shù)據(jù)集:

    https://github.com/mwaskom/seaborn-data

    放到本地:

    運(yùn)行程序:

    import?seaborn?as?snstips?=?sns.load_dataset("tips")print("tips:",tips) print("ype(tips):",type(tips))

    效果如下:

    參考博客:

    《解決seaborn導(dǎo)入數(shù)據(jù)集出現(xiàn)錯(cuò)誤》

    https://blog.csdn.net/qq_33828738/article/details/107044082

    5.2 seaborn繪制折線圖

    5.2.1 通過relplot來實(shí)現(xiàn)

    示例代碼:

    import?matplotlib.pyplot?as?plt import?seaborn?as?sns#?數(shù)據(jù)集 data?=?sns.load_dataset("fmri") print(data.head()) #?繪畫折線圖 sns.relplot(x="timepoint",?y="signal",?kind="line",?data=data,?ci=None) #?顯示 plt.show()

    效果如下:

    5.2.2 通過lineplot()函數(shù)來實(shí)現(xiàn)

    示例代碼:

    import?matplotlib.pyplot?as?plt import?seaborn?as?sns#?數(shù)據(jù)集 data?=?sns.load_dataset("fmri") print(data.head()) #?繪畫折線圖: sns.lineplot(x="timepoint",?y="signal",?data=data,?ci=95) #?顯示 plt.show()

    效果如下:

    5.2.3 多坐標(biāo)效果

    示例代碼:

    import?matplotlib.pyplot?as?plt import?seaborn?as?sns#?數(shù)據(jù)集 data?=?sns.load_dataset("fmri") print(data.head())#?繪畫折線圖 f,?axes?=?plt.subplots(nrows=1,?ncols=2,?figsize=(14,?6))sns.lineplot(x="timepoint",?y="signal",?data=data,?ci=None,?ax=axes[0]) sns.lineplot(x="timepoint",?y="signal",?hue="region",?style="event",?data=data,?ci=None,?ax=axes[1]) plt.show()

    效果如下:

    5.2.4 保存生成的圖片

    注意:需要在plt.show()之前調(diào)用savefig,不然保存的圖片就是一片空白

    plt.savefig('seaborn生成的圖片.png')plt.show()

    效果如下:

    5.3 seaborn replot 繪制散點(diǎn)圖

    示例代碼:

    import?matplotlib.pyplot?as?plt import?seaborn?as?sns#?準(zhǔn)備數(shù)據(jù):自帶數(shù)據(jù)集 tips?=?sns.load_dataset("tips") print(tips.head())#?繪畫散點(diǎn)圖 sns.relplot(x="total_bill",?y="tip",?data=tips,?hue="sex",?style="smoker",?size="size") sns.relplot(x="total_bill",?y="tip",?data=tips,?hue="sex",?style="smoker",?size="size",?sizes=(100,?100)) #?顯示 plt.show()

    效果如下:

    5.4 seaborn barplot繪制柱狀圖

    • 垂直

    示例代碼:

    import?matplotlib.pyplot?as?plt import?seaborn?as?sns#?顯示正負(fù)號(hào)與中文不顯示問題 plt.rcParams['axes.unicode_minus']?=?False sns.set_style('darkgrid',?{'font.sans-serif':['SimHei',?'Arial']})#?去除部分warning import?warnings warnings.filterwarnings('ignore')plt.figure(dpi=150) x?=?['金融','農(nóng)業(yè)','制造業(yè)','新能源'] y?=?[164,?86,?126,?53] sns.barplot(x,?y)plt.show()

    效果如下:

    • 水平

    調(diào)換橫縱坐標(biāo)位置即可

    plt.figure(dpi=150) x?=?['金融','農(nóng)業(yè)','制造業(yè)','新能源'] y?=?[164,?86,?126,?53] sns.barplot(y,x?) plt.show()

    6.python-pptx 插入圖片

    前提條件:

    示例代碼:

    from?pptx?import?Presentation from?pptx.util?import?Pt,Cm#?打開已存在ppt ppt?=?Presentation('6.python-pptx操作模板.pptx')#?設(shè)置添加到當(dāng)前ppt哪一頁(yè) n_page?=?0#?獲取需要添加文字的頁(yè)面對(duì)象 slide?=?ppt.slides[n_page]#?設(shè)置待添加的圖片 img_name??=?'seaborn生成的圖片.png' #?設(shè)置位置 left,?top,?width,?height?=?Cm(6),?Cm(6),?Cm(20),?Cm(9) #?進(jìn)行添加 slide.shapes.add_picture(image_file=img_name,left=left,top=top,width=width,height=height)#?保存ppt ppt.save('6.python-pptx?插入圖片.pptx')

    效果如下:

    7.python-pptx 讀取數(shù)據(jù)

    前提條件:

    準(zhǔn)備好一張有內(nèi)容的ppt

    示例代碼:

    from?pptx?import?Presentation from?pptx.enum.shapes?import?MSO_SHAPE_TYPE#?打開待讀取的ppt文件 ppt?=?Presentation('研發(fā)管理部檢測(cè)部周報(bào)2020-09-17.pptx')#?獲取第0張 slide0?=?ppt.slides[0]#?遍歷所有內(nèi)容 for?shape?in?slide0.shapes:#?打印shape名稱print(shape.shape_type)#?判斷是否為表格if?shape.shape_type?==?MSO_SHAPE_TYPE.TABLE:#獲取表格行for?row?in?shape.table.rows:for?cell?in?row.cells:print(cell.text_frame.text)

    效果如下:

    將當(dāng)前幻燈片頁(yè)面中的對(duì)象名稱和表格內(nèi)容全部打印出來了,反之,我們對(duì)其進(jìn)行復(fù)制,就是寫操作。

    < END >

    ●一文帶你回顧10大數(shù)據(jù)泄露事件!

    ●12000+字超詳細(xì) SQL 語法速成!

    后臺(tái)回復(fù)“入群”即可加入小z干貨交流群

    總結(jié)

    以上是生活随笔為你收集整理的用Python自动化操作PPT,看完这篇文章就够了!的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。