文章列表


python操作Excel之删除excel工作表

<p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">如果你想删除工作表,可以通过Workbook的remove()方法,但是这个方法的参数是一个Worksheet对象,所以如果你要使用这个方法删除工作表,前提是要先获取到一个工作表,获取工作表的方式我们前面已经学过了,但感觉这么做有点麻烦,很多情况我们希望直接根据表名来删除,这时候你可以通过Python的del关键字删除,这有点像通过键删除字典的项。代码如下:</p><pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; font-size: 13.6px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-top: 0px; overflow: auto; color: rgb(47, 111, 159); background-color: rgb(246, 246, 246); border: 1px solid rgb(238, 238, 238); padding: 10px; border-radius: 3px; overflow-wrap: break-word; text-wrap: wrap; margin-bottom: 0px !important;">#&nbsp;删除工作表from&nbsp;openpyxl import&nbsp;Workbookwb&nbsp;=&nbsp;Workbook()&nbsp;ws2&nbsp;=&nbsp;wb.create_sheet(&nbsp;&quot;表2&quot;) wb.create_sheet(&nbsp;&quot;表3&quot;) wb.create_sheet(&nbsp;&quot;表4&quot;) print(wb.sheetnames)#&nbsp;输出:[&nbsp;&#39;Sheet&#39;,&nbsp;&#39;表2&#39;,&nbsp;&#39;表3&#39;,&nbsp;&#39;表4&#39;] wb.remove(ws2)&nbsp;del&nbsp;wb[&nbsp;&quot;表3&quot;] print(wb.sheetnames)#&nbsp;输出:[&nbsp;&#39;Sheet&#39;,&nbsp;&#39;表4&#39;]</pre><p><br/></p>

python操作Excel之新建excel工作表

<p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">很多情况下一张表不够用,新建工作表就很有必要了。Workbook对象提供了create_sheet()方法用于新建工作表,它需要两个参数,第一个是表名,第二个是表的位置,位置从0开始,这还是比较符合我们的编程逻辑的,如果你不指定位置,则默认添加到最后,调用完create_sheet()方法之后会返回新创建的工作表,即Worksheet对象。代码如下:</p><pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; font-size: 13.6px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-top: 0px; overflow: auto; color: rgb(47, 111, 159); background-color: rgb(246, 246, 246); border: 1px solid rgb(238, 238, 238); padding: 10px; border-radius: 3px; overflow-wrap: break-word; text-wrap: wrap; margin-bottom: 0px !important;">from&nbsp;openpyxl import&nbsp;load_workbookwb&nbsp;=&nbsp;load_workbook(&nbsp;&quot;./excel_&nbsp;files/全国城市某天最高温度排行.xlsx&quot;) print(wb.sheetnames)#&nbsp;输出:[&nbsp;&#39;最高温度排名&#39;,&nbsp;&#39;最低温度排名&#39;] new_sheet1&nbsp;=&nbsp;wb.create_sheet(&nbsp;&quot;新建表1&quot;,&nbsp;0)&nbsp;new_sheet2&nbsp;=&nbsp;wb.create_sheet(&nbsp;&quot;新建表2&quot;) print(wb.sheetnames)#&nbsp;输出:[&#39;新建表1&#39;,&nbsp;&#39;最高温度排名&#39;,&nbsp;&#39;最低温度排名&#39;,&nbsp;&#39;新建表2&#39;]&nbsp;print(new_sheet1.title)#&nbsp;输出:&nbsp;新建表1print(new_sheet2.title)#&nbsp;输出:&nbsp;新建表2</pre><p><br/></p>

python操作word之处理文档中的间距和缩进

<p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">段落与段落之间可以设置距离,比如说段前16磅,段后20磅,这里就需要用到长度单位了,我们以后再学习python-docx的长度单位,现在先提前用一下“磅”这个单位,用Pt这个类表示,实例化对象的时候传入磅数即可,比如说Pt(20)就表示20磅。</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">有了长度单位之后我们就可以设置段落的距离了。首先要知道Paragraph对象用ParagraphFormat这个类来管理样式,前面我们学习对齐的时候设置的alignment属性其实就是ParagraphFormat对象的,这次我们来修改一下ParagraphFormat对象的space_before和space_after属性,这两个属性分别表示段前和段后的距离,看一下我的代码:</p><pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; font-size: 13.6px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-top: 0px; margin-bottom: 16px; overflow: auto; color: rgb(47, 111, 159); background-color: rgb(246, 246, 246); border: 1px solid rgb(238, 238, 238); padding: 10px; border-radius: 3px; overflow-wrap: break-word; text-wrap: wrap;">from&nbsp;docx import&nbsp;Documentfrom&nbsp;docx.shared import&nbsp;Ptdoc&nbsp;=&nbsp;Document()&nbsp;paragraph&nbsp;=&nbsp;doc.add_paragraph(&nbsp;&quot;某一天Of&nbsp;fice遇到了Python&quot;) paragraph.paragraph_format.space_before&nbsp;=&nbsp;Pt(16)#&nbsp;段前16磅paragraph.paragraph_format.space_after&nbsp;=&nbsp;Pt(20)#&nbsp;段后20磅doc.save(&quot;./word_&nbsp;files/test.docx&quot;)</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">当然ParagraphFormat对象肯定没那么简单,它还有其他的属性,比如说f i rst_line_indent表示首行缩进,line_spacing表示行间距,代码如下:</p><pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; font-size: 13.6px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-top: 0px; margin-bottom: 16px; overflow: auto; color: rgb(47, 111, 159); background-color: rgb(246, 246, 246); border: 1px solid rgb(238, 238, 238); padding: 10px; border-radius: 3px; overflow-wrap: break-word; text-wrap: wrap;">...paragraph&nbsp;=&nbsp;doc.add_paragraph(&quot;某一天Of&nbsp;fice遇到了Python&quot;)paragraph.paragraph_format.line_spacing&nbsp;=&nbsp;Pt(20)&nbsp;&nbsp;#&nbsp;行间距,固定值20磅paragraph.paragraph_format.first_line_indent&nbsp;=&nbsp;Pt(10)&nbsp;&nbsp;#&nbsp;首行缩进10磅</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">说到行间距,如果直接指定line_spacing的值,那就是固定行间距,如果指定一个数字就表示多倍行距,比如说1.5倍行距、2倍行距、3倍行距等,代码如下:</p><pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; font-size: 13.6px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-top: 0px; overflow: auto; color: rgb(47, 111, 159); background-color: rgb(246, 246, 246); border: 1px solid rgb(238, 238, 238); padding: 10px; border-radius: 3px; overflow-wrap: break-word; text-wrap: wrap; margin-bottom: 0px !important;">...paragraph.paragraph_format.line_spacing&nbsp;=&nbsp;1.5&nbsp;&nbsp;#&nbsp;1.5倍行距paragraph.paragraph_format.line_spacing&nbsp;=&nbsp;2&nbsp;&nbsp;#&nbsp;2倍行距</pre><p><br/></p>

python操作word之使用word内置样式操作

<p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">Word有一些内置样式,在Word里面显示为“预设样式”,我们可以直接拿来使用。内置样式非常多,但样式类型大致可以分为四种,分别是字符样式、列表样式、段落样式、表格样式,它们定义在WD_STYLE_TYPE这个枚举类里。我们这里先拿段落样式进行演示,代码如下:</p><pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; font-size: 13.6px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-top: 0px; margin-bottom: 16px; overflow: auto; color: rgb(47, 111, 159); background-color: rgb(246, 246, 246); border: 1px solid rgb(238, 238, 238); padding: 10px; border-radius: 3px; overflow-wrap: break-word; text-wrap: wrap;">from&nbsp;docx import&nbsp;Documentdoc&nbsp;=&nbsp;Document()#&nbsp;列表paragraph&nbsp;=&nbsp;doc.add_paragraph(&nbsp;&quot;有序段落List&quot;) print(paragraph.style.name)#&nbsp;输出:&nbsp;Normalparagraph.style&nbsp;=&nbsp;&quot;List&quot; doc.add_paragraph(&nbsp;&quot;有序段落List&nbsp;2&quot;,&nbsp;style&nbsp;=&nbsp;&quot;List&nbsp;2&quot;) doc.add_paragraph(&nbsp;&quot;有序段落List&nbsp;3&quot;,&nbsp;style&nbsp;=&nbsp;&quot;List&nbsp;3&quot;)# 列表编号doc.add_paragraph(&nbsp;&quot;有序段落List&nbsp;Number&quot;,&nbsp;style&nbsp;=&nbsp;&quot;List&nbsp;Number&quot;) doc.add_paragraph(&nbsp;&quot;有序段落List&nbsp;Number&nbsp;2&quot;,&nbsp;style&nbsp;=&nbsp;&quot;List&nbsp;Number&nbsp;2&quot;) doc.add_paragraph(&nbsp;&quot;有序段落List&nbsp;Number&nbsp;3&quot;,&nbsp;style&nbsp;=&nbsp;&quot;List&nbsp;Number&nbsp;3&quot;)# 列表项目符号doc.add_paragraph(&nbsp;&quot;无序段落List&nbsp;Bullet&quot;,&nbsp;style&nbsp;=&nbsp;&quot;List&nbsp;Bullet&quot;) doc.add_paragraph(&nbsp;&quot;无序段落List&nbsp;Bullet&nbsp;2&quot;,&nbsp;style&nbsp;=&nbsp;&quot;List&nbsp;Bullet&nbsp;2&quot;) doc.add_paragraph(&nbsp;&quot;无序段落List&nbsp;Bullet&nbsp;3&quot;,&nbsp;style&nbsp;=&nbsp;&quot;List&nbsp;Bullet&nbsp;3&quot;) doc.save(&quot;./word_&nbsp;files/test.docx&quot;)</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">执行上面的代码之后得到的效果如图7-1所示。<br/><img src="https://www.maxiaoke.com/uploads/images/20231205/4868128ff1a9158267c7907801a02abe.png" alt=""/></p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">Paragraph对象有一个style属性控制段落样式,我们只要把样式名赋值给该属性就可以引用样式。当然也可以在调用add_paragraph()方法新增段落的时候就引用样式,该方法的第二个参数就是样式名。如果不设置段落的样式,则默认样式名是“Normal”,也就是Word里的第一个内置样式“正文”。</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">还要注意样式名是一个字符串,这些内置的样式名都是固定的,所以不能写错了。既然样式名是固定的,那我们怎么查看都有哪些样式呢?我们干脆读取Document的styles取出全部样式,然后过滤出类型为段落的样式,代码如下:</p><pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; font-size: 13.6px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-top: 0px; margin-bottom: 16px; overflow: auto; color: rgb(47, 111, 159); background-color: rgb(246, 246, 246); border: 1px solid rgb(238, 238, 238); padding: 10px; border-radius: 3px; overflow-wrap: break-word; text-wrap: wrap;">from&nbsp;docx import&nbsp;Documentfrom&nbsp;docx.enum.style import&nbsp;WD_STYLE_TYPEdoc&nbsp;=&nbsp;Document()&nbsp;styles&nbsp;=&nbsp;doc.stylesparagraph_styles&nbsp;=&nbsp;[s &nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;s&nbsp;in&nbsp;styles &nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;s.type&nbsp;==&nbsp;WD_STYLE_TYPE.PARAGRAPH ] for&nbsp;style&nbsp;in&nbsp;paragraph_styles:&nbsp;print(style.name)#&nbsp;输出:#&nbsp;Normal#&nbsp;Header#&nbsp;Footer#&nbsp;Heading&nbsp;1#&nbsp;Heading&nbsp;2#...</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">代码中通过列表推导式拿到全部内置段落样式,一共有36个。每一个样式对应的类都是继承BaseStyle类的,BaseStyle对象的type属性用于区分样式类型,所以我们让type等于 WD_STYLE_TYPE.PARAGRAPH的样式保留下来,而name属性表示样式名,把它打印出来看一下,我们在引用样式的时候就直接写这些样式名就可以了。</p><p style="box-sizing: border-box; margin-top: 0px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap; margin-bottom: 0px !important;">关于WD_STYLE_TYPE,它是一个枚举类,这里一共定义了四种类型,分别是CHARACTER(字符)、LIST(列表)、PARAGRAPH(段落)和TABLE(表格),如果以后有需要的话可以按照这种方式进行过滤。</p><p><br/></p>

python与办公之PPT功能实现新建幻灯片

<p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">在python-pptx中,每一个幻灯片页面都是一个Slide对象,Presentation对象并不能直接创建Slide对象,但它能创建Slides对象。听这个名字就知道Slides是用来管理Slide的,没错,Slides对象有一个add_slide()方法可以创建Slide对象,add_slide()方法的参数是一个SlideLayout对象。简单来说,要创建幻灯片,首先要获取布局,然后再调用Presentation对象的slides属性获得一个Slides对象,再调用add_slide()方法,演示代码如下:</p><pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; font-size: 13.6px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-top: 0px; margin-bottom: 16px; overflow: auto; color: rgb(47, 111, 159); background-color: rgb(246, 246, 246); border: 1px solid rgb(238, 238, 238); padding: 10px; border-radius: 3px; overflow-wrap: break-word; text-wrap: wrap;">from&nbsp;pptx import&nbsp;Presentationppt&nbsp;=&nbsp;Presentation()&nbsp;slide_layout&nbsp;=&nbsp;ppt.slide_layouts[0]&nbsp;print(type(ppt.slides))#&nbsp;&lt;&nbsp;class&nbsp;&#39;pptx.slide.Slides&#39;&nbsp;&gt;&nbsp;slide&nbsp;=&nbsp;ppt.slides.add_slide(slide_layout)&nbsp;print(type(slide))#&nbsp;输出:&nbsp;&lt;&nbsp;class&nbsp;&#39;pptx.slide.Slide&#39;&nbsp;&gt;&nbsp;ppt.save(&quot;./ppt_&nbsp;files/test.pptx&quot;)</pre><p style="box-sizing: border-box; margin-top: 0px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap; margin-bottom: 0px !important;">通过add_slide()这种方法添加的幻灯片只能添加到文档的最后面,目前没有办法在两个幻灯片之间插入其他幻灯片,希望以后的版本会实现。</p><p><br/></p>

python与办公之PPT功能实现操作幻灯片布局

<p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">每一个幻灯片页面都必须要指定一个幻灯片布局,那么什么是布局呢?幻灯片布局就是指幻灯片的模板,PPT里面把它叫作母版,一般来说一个母版上面会存在一些文本框、形状、图表等空白控件。当使用这个母版添加一张幻灯片的时候,这张幻灯片上面就会出现母版里已有的控件,这样做的好处是节省了添加控件和排版的步骤。</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">在python-pptx中,母版就是布局(layout),我们可以访问Presentation对象的slide_layouts属性获取该文档全部布局,它是一个可迭代对象,我们可以通过遍历它取得所有的单个布局,每一个布局都是一个SlideLayout对象,代码如下:</p><pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; font-size: 13.6px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-top: 0px; margin-bottom: 16px; overflow: auto; color: rgb(47, 111, 159); background-color: rgb(246, 246, 246); border: 1px solid rgb(238, 238, 238); padding: 10px; border-radius: 3px; overflow-wrap: break-word; text-wrap: wrap;">from&nbsp;pptx import&nbsp;Presentationppt&nbsp;=&nbsp;Presentation()&nbsp;print(len(ppt.slide_layouts))#&nbsp;输出:&nbsp;11 for&nbsp;layout&nbsp;in&nbsp;ppt.slide_layouts:&nbsp;print(type(layout))#&nbsp;输出:&nbsp;&lt;&nbsp;class&nbsp;&#39;pptx.slide.SlideLayout&#39;&nbsp;&gt;</pre><p style="box-sizing: border-box; margin-top: 0px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap; margin-bottom: 0px !important;">我偷偷打印了slide_layouts的长度,发现是11,也就是说默认的PPT文档一共有11种布局,至于这11种布局到底长什么样,我们可以打开PPT软件看看。单击PPT软件的“视图”选项卡,单击“幻灯片母版”按钮即可看到默认的页面母版,如图8-1所示。<br/><img src="https://www.maxiaoke.com/uploads/images/20231205/995a4c5c96fdd30523d888bf2946f590.png" alt=""/><br/>我们晚点再学习关于SlideLayout对象的其他知识点,这里主要是先提前跟布局打个招呼,因为等会儿新建幻灯片的时候需要用到布局。</p><p><br/></p>

python操作pdf之实现PDF页面绽放功能

<p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">每一个PDF页面都有自己的尺寸,在pypdf2中,PageObject对象的mediabox属性存储的是就是该页面的尺寸信息,对应的是一个RectangleObject对象。RectangleObject对象的left、bottom、right、top四个属性分别代表左、下、右、上四边的距离,我们可以把一个PDF页面的左下角当成是坐标原点,而RectangleObject的四个属性分别是四条边到达坐标轴的距离。</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">我还是举一个例子看一下吧:</p><pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; font-size: 13.6px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-top: 0px; margin-bottom: 16px; overflow: auto; color: rgb(47, 111, 159); background-color: rgb(246, 246, 246); border: 1px solid rgb(238, 238, 238); padding: 10px; border-radius: 3px; overflow-wrap: break-word; text-wrap: wrap;">from&nbsp;PyPDF2 import&nbsp;PdfReaderreader&nbsp;=&nbsp;PdfReader(&nbsp;&quot;./pdf_&nbsp;files/练习文档.pdf&quot;) page&nbsp;=&nbsp;reader.getPage(0)&nbsp;print(page.mediabox)#&nbsp;输出:&nbsp;RectangleObject([0,&nbsp;0,&nbsp;612,&nbsp;792])&nbsp;print(page.mediabox.left)#&nbsp;输出:&nbsp;0&nbsp;print(page.mediabox.bottom)#&nbsp;输出:&nbsp;0&nbsp;print(page.mediabox.right)#&nbsp;输出:&nbsp;612&nbsp;print(page.mediabox.top)#&nbsp;输出:&nbsp;792</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">代码中读取了“练习文档.pdf”的第一页的mediabox属性,该文档是信纸的尺寸,约等于612磅×792磅,我们把它放到坐标图里看一下,如图9-2所示。<br/><img src="https://www.maxiaoke.com/uploads/images/20231206/b47abf662849357b1e2d8c7c7db9286e.png" alt=""/></p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">现在通过坐标图我们可以更容易理解RectangleObject左、下、右、上四个属性所表示的含义了,所以修改这几个属性实际上就是修改页面的尺寸。</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">比如说我们要把信纸的尺寸修改为A4纸的尺寸,只要把PageObject对象的mediabox 重新赋值为一个指定四边距离的RectangleObject对象即可,注意这四边距离是一个元组,代码如下:</p><pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; font-size: 13.6px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-top: 0px; margin-bottom: 16px; overflow: auto; color: rgb(47, 111, 159); background-color: rgb(246, 246, 246); border: 1px solid rgb(238, 238, 238); padding: 10px; border-radius: 3px; overflow-wrap: break-word; text-wrap: wrap;">from&nbsp;PyPDF2 import&nbsp;PdfReader,&nbsp;PdfWriterfrom&nbsp;PyPDF2.generic import&nbsp;RectangleObjectreader&nbsp;=&nbsp;PdfReader(&nbsp;&quot;./pdf_&nbsp;files/练习文档.pdf&quot;) writer&nbsp;=&nbsp;PdfWriter()&nbsp;for&nbsp;page&nbsp;in&nbsp;reader.pages:&nbsp;page.mediabox&nbsp;=&nbsp;RectangleObject((0,&nbsp;0,&nbsp;595.28,&nbsp;841.89))&nbsp;writer.addPage(page)&nbsp;with&nbsp;open(&quot;./pdf_&nbsp;files/test.pdf&quot;,&nbsp;&quot;wb&quot;)&nbsp;as&nbsp;f:&nbsp;writer.write(f)</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">如果不想自己实例化RectangleObject对象也是可以的,PageObject对象有提供一些缩放方法,比如说scaleTo()方法可以直接指定宽高,代码如下:</p><pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; font-size: 13.6px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-top: 0px; margin-bottom: 16px; overflow: auto; color: rgb(47, 111, 159); background-color: rgb(246, 246, 246); border: 1px solid rgb(238, 238, 238); padding: 10px; border-radius: 3px; overflow-wrap: break-word; text-wrap: wrap;">from&nbsp;PyPDF2 import&nbsp;PdfReader,&nbsp;PdfWriterreader&nbsp;=&nbsp;PdfReader(&nbsp;&quot;./pdf_&nbsp;files/练习文档.pdf&quot;) writer&nbsp;=&nbsp;PdfWriter()&nbsp;for&nbsp;page&nbsp;in&nbsp;reader.pages:&nbsp;page.scaleTo(595.28,&nbsp;841.89)&nbsp;writer.addPage(page)&nbsp;with&nbsp;open(&quot;./pdf_&nbsp;files/test.pdf&quot;,&nbsp;&quot;wb&quot;)&nbsp;as&nbsp;f:&nbsp;writer.write(f)</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">PageObject对象的scaleBy()方法可以按某个比例因子对页面进行等比缩放,比例因子就是指缩放的倍数,比如说把所有页面的宽高都缩小为原来的50%,代码如下:</p><pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; font-size: 13.6px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-top: 0px; overflow: auto; color: rgb(47, 111, 159); background-color: rgb(246, 246, 246); border: 1px solid rgb(238, 238, 238); padding: 10px; border-radius: 3px; overflow-wrap: break-word; text-wrap: wrap; margin-bottom: 0px !important;">from&nbsp;PyPDF2 import&nbsp;PdfReader,&nbsp;PdfWriterreader&nbsp;=&nbsp;PdfReader(&nbsp;&quot;./pdf_&nbsp;files/练习文档.pdf&quot;) writer&nbsp;=&nbsp;PdfWriter()&nbsp;for&nbsp;page&nbsp;in&nbsp;reader.pages:&nbsp;page.scaleBy(0.5)&nbsp;writer.addPage(page)&nbsp;with&nbsp;open(&quot;./pdf_&nbsp;files/test.pdf&quot;,&nbsp;&quot;wb&quot;)&nbsp;as&nbsp;f:&nbsp;writer.write(f)</pre><p><br/></p>

python操作PDF之旋转页面功能实现

<p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">PageObject对象有一个rotate()方法可以用于对自身进行旋转,旋转角度取值是0、90、270、180等,即90的倍数,正数是顺时针,负数是逆时针。现在有一个需求是要把一个PDF文件的奇数页逆时针旋转90度,偶数页顺时针旋转90度。</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">我们可以使用rotate()方法旋转页面,所以这个问题的关键是怎么判断当前页面对应页数的奇偶呢?肯定是需要获取文档的总页数,之后遍历所有页面,把当前页数对2取余,结果为0则为偶数,否则是奇数。不过要注意,如果是使用range()方法生成下标,计算奇偶之前记得加上1,因为range()默认是从0开始的,代码如下:</p><pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; font-size: 13.6px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-top: 0px; margin-bottom: 16px; overflow: auto; color: rgb(47, 111, 159); background-color: rgb(246, 246, 246); border: 1px solid rgb(238, 238, 238); padding: 10px; border-radius: 3px; overflow-wrap: break-word; text-wrap: wrap;">from&nbsp;PyPDF2 import&nbsp;PdfReader,&nbsp;PdfWriterreader&nbsp;=&nbsp;PdfReader(&nbsp;&quot;./pdf_&nbsp;files/练习文档.pdf&quot;) writer&nbsp;=&nbsp;PdfWriter()&nbsp;for&nbsp;i&nbsp;in&nbsp;range(reader.getNumPages()):&nbsp;if(i&nbsp;+&nbsp;1)&nbsp;%&nbsp;2&nbsp;==&nbsp;0:&nbsp;page&nbsp;=&nbsp;reader.getPage(i).rotate(90) &nbsp;&nbsp;&nbsp;&nbsp;else&nbsp;:page&nbsp;=&nbsp;reader.getPage(i).rotate(-90)&nbsp;writer.addPage(page)&nbsp;with&nbsp;open(&quot;./pdf_&nbsp;files/test.pdf&quot;,&nbsp;&quot;wb&quot;)&nbsp;as&nbsp;f:&nbsp;writer.write(f)</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">既然讲到这里了,那就趁机再讲一下怎么知道某个PageObject对象对应的下标。PdfReader对象有一个get_page_number()方法,调用时只要把一个PageObject对象传进去就会返回该PageObject是第几页了,所以上面的旋转需求我们也可以这样写:</p><pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; font-size: 13.6px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-top: 0px; overflow: auto; color: rgb(47, 111, 159); background-color: rgb(246, 246, 246); border: 1px solid rgb(238, 238, 238); padding: 10px; border-radius: 3px; overflow-wrap: break-word; text-wrap: wrap; margin-bottom: 0px !important;">from&nbsp;PyPDF2 import&nbsp;PdfReader,&nbsp;PdfWriterreader&nbsp;=&nbsp;PdfReader(&nbsp;&quot;./pdf_&nbsp;files/练习文档.pdf&quot;) writer&nbsp;=&nbsp;PdfWriter()&nbsp;for&nbsp;page&nbsp;in&nbsp;reader.pages:&nbsp;page_num&nbsp;=&nbsp;reader.get_page_number(page)&nbsp;page&nbsp;=&nbsp;page.rotate(90)&nbsp;if(page_num&nbsp;+&nbsp;1)&nbsp;%&nbsp;2&nbsp;==&nbsp;0 &nbsp;&nbsp;&nbsp;&nbsp;else&nbsp;page.rotate(-90)&nbsp;writer.addPage(page)&nbsp;with&nbsp;open(&quot;./pdf_&nbsp;files/test.pdf&quot;,&nbsp;&quot;wb&quot;)&nbsp;as&nbsp;f:&nbsp;writer.write(f)</pre><p><br/></p>

详细介绍Python线程同步的实现

<p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">严格来说,在Python中,我们并不需要专门去实现线程同步,因为在Python中编写多线程程序时,无论是否手动给Python多线程程序加锁,在多线程程序运行时,都会被GIL所影响,使原本的线程不同步转变为安全的线程同步,不需要开发者额外手动进行处理。在本节中,笔者不会对Python中具体的线程同步实现原理进行介绍,而是对CPython官方实现Python线程同步以保证Python线程安全的整体概念和实现思路进行补充介绍。</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">Python线程同步指的是在多线程环境中执行任意一段Python代码(这段代码可以是一个方法,也可以是一个类,还可以是一个接口等),均能返回期望的结果。对于Python多线程环境下的资源竞争过程,笔者画了一张示意图,如图3-1所示。</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;"><img src="https://www.maxiaoke.com/uploads/images/20231206/41deefca3ed0bafd794b841163a30b19.png" alt=""/></p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">假设在Python环境中同时存在线程A、线程B、线程C,且都需要执行同一个任务,假设这个任务的名称为T,这三个线程需要同时执行任务T,以达到多线程执行任务的目的。对于CPU来说,任一线程想要执行具体的任务,都需要CPU进行统一调度;对于线程来说,需要首先获取执行任务所需要的资源,才能开始执行。</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">在没有线程同步概念下,线程A、线程B、线程C在获取临界区中的资源时就会随机获取,甚至会出现都获取不到资源的情况,所以在图3-1的输出部分,笔者使用“线程?”的形式来描述在没有线程同步概念下,任务能不能被执行以及是否按照我们所设计的时序来执行都是不确定的。那么,在线程同步概念下,上述任务执行过程将会是什么样的呢?笔者也画了一张图,如图3-2所示。</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;"><img src="https://www.maxiaoke.com/uploads/images/20231206/6255422a576b86881bd67931cb5b0305.png" alt=""/></p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">在线程同步概念下,线程A、线程B、线程C访问临界区中资源的方式不变,只不过在任意时刻只能有一个线程可以获取到临界区中的资源。这里以线程A为例,线程A在获取临界区中的资源之后,就会以标记位的方式将临界区中的相关资源进行占有性标记。</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">在标记成功后,线程B、线程C都不能获取该资源。接着,线程A使用该资源继续执行任务,直到任务执行完毕,线程A才会释放资源。在线程A释放资源后,线程B或者线程C才能获取该资源,并执行后续任务。如此反复执行上述过程,直到所有的线程都获取该资源,并且任务均被执行完毕为止。</p><p style="box-sizing: border-box; margin-top: 0px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap; margin-bottom: 0px !important;">这是CPython官方在实现线程同步时使用的主要思路,并且将线程同步和线程安全放在一起实现,所以,读者在阅读本小节内容时,可以结合3.3.1节一起看。</p><p><br/></p>

一篇文章详细讲解Python线程安全性的实现

<p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">Python线程安全性指的是在Python环境中,多个线程可以并发或并行地执行Python开发者分配的任务,并且任务执行完毕后的结果总是正确的。在多线程执行Python任务时,但凡有一次任务没有得到预期的结果,就不能说明我们所设计的Python多线程程序是安全的。</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">CPython官方在实现Python线程安全时,将GIL进行了封装,并且将其实现过程封装到Python的内核实现层面,具体的实现源代码文件名称为pycore_gil.h,即CPython官方将GIL的实现过程封装成C语言的头文件。pycore_gil.h文件中的源代码如下。</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;"><img src="https://www.maxiaoke.com/uploads/images/20231206/f0f565bbcbb6ab12b45187290c7b3a54.png" alt=""/></p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">首先来看pycore_gil.h头文件中的定义,Py_INTERNAL_GIL_H是GIL的内核实现定义。在本文件中通过取反的方式进行引入,即先判断Py_INTERNAL_GIL_H没有引入该文件,如果这一判断结果为真,则引入Py_INTERNAL_GIL_H;如果这一判断结果为假,则不再重复引入Py_INTERNAL_GIL_H。__cplusplus是在C语言文件中定义C++拓展程序的语法形式,由以下代码段组成:<br/><img src="https://www.maxiaoke.com/uploads/images/20231206/3a802cb54fad6c50150c55a80f8d6a6a.png" alt=""/></p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">pycore_gil.h头文件中还有几个地方需要注意。首先来看Py_BUILD_CORE,Py_BUILD_CORE是Python解释器识别Python内核文件的标识。如果Python内核文件中没有该标识,编译Python程序时就会提示“this header requires Py_BUILD_CORE define”错误,即只有文件中添加了Py_BUILD_CORE标识,才能被Python解释器识别为内核文件,并在内核编译期进行编译。pycore_atomic.h是Python实现原子性的内核头文件。</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">该文件可以实现Python程序全局的原子性,也可以实现具体Python操作的原子性。FORCE_SWITCHING表示线程及线程上下文的切换方式。该切换方式有两种:一种是操作系统CPU内核自动管理和切换线程及线程上下文,该方式也是CPython官方实现线程及线程上下文切换的默认方式;另一种是手动强制切换,我们可以在运行Python程序时,通过向Python解释器配置FORCE_SWITCHING参数来改变Python线程及线程上下文的切换方式为手动强制切换。</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">接着来看名为_gil_runtime_state的结构体定义,它是GIL的核心实现和调用的封装。_gil_runtime_state结构体分别由无符号long类型的变量interval、_Py_atomic_address类型的变量last_holder、_Py_atomic_int类型的变量locked、无符号long类型的变量switch_number、PyCOND_T类型的变量cond、PyMUTEX_T类型的变量mutex,以及对FORCE_SWITCHING状态的判断组成。该结构体规定了GIL的运行流程和管理措施。</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">变量interval表示GIL的执行时间,单位为μs,且不能是负数。在GIL被触发时,变量interval随即开始工作,直到GIL执行完毕。这个时间间隔就是GIL的执行时间,开发者可以通过该变量获取GIL执行程序所消耗的时间。变量last_holder表示最后持有GIL的标记地址,开发者可以通过该变量来查看最后持有GIL的Python程序。</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">变量locked表示Python程序是否被GIL修饰,如果没有被修饰,说明Python程序没有获取到GIL, locked变量值为-1,否则不为-1,而是一个大于0的值。同样,该变量是原子性变量,开发者可以通过该变量的值来判断Python程序有没有获取GIL。</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">变量switch_number表示GIL的切换次数,即锁被获取和锁被释放之间的切换次数。该变量不能是负数,只能是大于0的正整数。开发者可以通过该变量的值来查看Python程序中GIL的切换次数,从而判断Python线程是否正常运转。变量cond表示条件的意思,可以理解为影响线程安全性的条件。该变量支持设置允许一个或多个线程等待,直到获取到GIL的线程释放锁了之后,逐个解除等待。</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">开发者可以通过查看该变量是否有数据来判断线程是否处于等待状态,如果有数据,表明线程仍然处于等待状态。变量mutex表示Python中互斥锁的实现标记位。Python通过引入该变量来保证在同一时刻只能允许一个线程获取GIL,并且对获取GIL的线程进行互斥保护。</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">最后来看_gil_runtime_state结构体的最后部分,代码如下所示。<br/><img src="https://www.maxiaoke.com/uploads/images/20231206/1ad2870bf25e85293f60f6311b5b2984.png" alt=""/><br/>上述代码段表示如果开发者启用了线程和线程上下文手动强制切换方式,那么GIL在运行时就会同步开启手动Python线程的切换和手动Python互斥锁的切换,Python解释器不会再自动管理Python线程的切换和Python互斥锁的切换;如果开发者没有启用手动强制切换方式,GIL在运行时并不会将手动Python线程的切换和手动Python互斥锁的切换进行应用。</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap;">通过上述_gil_runtime_state结构体的定义,我们实现了GIL的整体管理。在编写Python程序时,开发者并不需要了解Python GIL的内部运转过程,只需要清楚在默认情况下Python线程被GIL保护着。</p><p style="box-sizing: border-box; margin-top: 0px; color: rgb(77, 82, 89); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; text-wrap: wrap; margin-bottom: 0px !important;">通过分析上述GIL实现的结构体_gil_runtime_state可知,Python在实现线程安全时,首先为每个访问临界区的Python线程统一添加GIL,然后通过变量interval和变量last_holder的相互配合,来确定每一个线程的执行时间和最大执行时间边界值,如果发现某个线程的执行时间太长,则会自动终止该线程的运行,并通过变量locked来改变多线程的状态。在多线程执行过程中,Python通过变量cond和变量mutex来规定每个线程的执行时机和线程状态切换时机,最后通过变量switch_number在后台静默记录每个线程对应的切换次数,以备不时之需。</p><p><br/></p>