<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>python-excel归档 - 帝讯博客</title>
	<atom:link href="https://www.dixunblog.cn/tag/python-excel/feed" rel="self" type="application/rss+xml" />
	<link>https://www.dixunblog.cn/tag/python-excel</link>
	<description>致力于打造专业的互联网资讯平台</description>
	<lastBuildDate>Mon, 30 Mar 2026 03:48:46 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0</generator>

<image>
	<url>https://cdn.hyclive.cn/dixunblog/2025/12/cropped-ico-32x32.png</url>
	<title>python-excel归档 - 帝讯博客</title>
	<link>https://www.dixunblog.cn/tag/python-excel</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Python 自动化办公实战：Excel 处理 &#8211; 基础篇</title>
		<link>https://www.dixunblog.cn/1453.html</link>
					<comments>https://www.dixunblog.cn/1453.html#respond</comments>
		
		<dc:creator><![CDATA[小编]]></dc:creator>
		<pubDate>Mon, 30 Mar 2026 02:05:55 +0000</pubDate>
				<category><![CDATA[技术教程]]></category>
		<category><![CDATA[编程代码]]></category>
		<category><![CDATA[Python 教程]]></category>
		<category><![CDATA[python-excel]]></category>
		<category><![CDATA[代码教程]]></category>
		<guid isPermaLink="false">https://www.dixunblog.cn/1453.html</guid>

					<description><![CDATA[<p>欢迎来到今天的 Python 实战教程！今天我们来学习 Python 自动化办公之 Excel 处理。</p>
<p>一、环境准备</p>
<p>1.1 安装库<br />
<code class="language-bash">pip install openpyxl pandas xlrd xlwt</p>
<p>1.2 库的选择</p>
<p><strong>openpy...</p>
<p><a href="https://www.dixunblog.cn/1453.html">Python 自动化办公实战：Excel 处理 &#8211; 基础篇</a>最先出现在<a href="https://www.dixunblog.cn">帝讯博客</a>。</p>
]]></description>
										<content:encoded><![CDATA[<p>欢迎来到今天的 Python 实战教程！今天我们来学习 Python 自动化办公之 Excel 处理。</p>
<h2><img fetchpriority="high" decoding="async" class="alignnone size-full wp-image-1442" src="http://cdn.hyclive.cn/dixunblog/2026/03/生成Python办公插图.png" alt="" width="2730" height="1535" /></h2>
<h2>一、环境准备</h2>
<h3>1.1 安装库</h3>
<pre><code class="language-bash">pip install openpyxl pandas xlrd xlwt</code></pre>
<h3>1.2 库的选择</h3>
<ul>
<li><strong>openpyxl</strong>：读写.xlsx 文件</li>
<li><strong>pandas</strong>：数据处理和分析</li>
<li><strong>xlrd/xlwt</strong>：读写.xls 文件（旧格式）</li>
</ul>
<h2>二、读取 Excel 文件</h2>
<h3>2.1 使用 openpyxl</h3>
<pre><code class="language-python">from openpyxl import load_workbook

# 加载工作簿
wb = load_workbook('data.xlsx')

# 选择工作表
ws = wb['Sheet1']

# 读取单元格
value = ws['A1'].value

# 遍历行
for row in ws.iter_rows():
    for cell in row:
        print(cell.value)</code></pre>
<h3>2.2 使用 pandas</h3>
<pre><code class="language-python">import pandas as pd

# 读取 Excel
df = pd.read_excel('data.xlsx')

# 查看数据
print(df.head())
print(df.columns)

# 选择列
names = df['姓名']</code></pre>
<h2>三、写入 Excel 文件</h2>
<h3>3.1 创建新文件</h3>
<pre><code class="language-python">from openpyxl import Workbook

wb = Workbook()
ws = wb.active
ws.title = "数据表"

# 写入数据
ws['A1'] = "姓名"
ws['B1'] = "年龄"
ws.append(["张三", 25])
ws.append(["李四", 28])

wb.save('output.xlsx')</code></pre>
<h3>3.2 使用 pandas 写入</h3>
<pre><code class="language-python">import pandas as pd

data = {
    '姓名': ['张三', '李四'],
    '年龄': [25, 28],
    '城市': ['北京', '上海']
}

df = pd.DataFrame(data)
df.to_excel('output.xlsx', index=False)</code></pre>
<h2>四、数据处理实战</h2>
<h3>4.1 数据筛选</h3>
<pre><code class="language-python"># 筛选年龄大于 25 的记录
filtered = df[df['年龄'] &gt; 25]

# 多条件筛选
filtered = df[(df['年龄'] &gt; 25) &amp; (df['城市'] == '北京')]</code></pre>
<h3>4.2 数据统计</h3>
<pre><code class="language-python"># 平均值
avg_age = df['年龄'].mean()

# 分组统计
grouped = df.groupby('城市')['年龄'].mean()</code></pre>
<h3>4.3 数据合并</h3>
<pre><code class="language-python"># 合并两个 Excel
df1 = pd.read_excel('file1.xlsx')
df2 = pd.read_excel('file2.xlsx')

# 横向合并
merged = pd.merge(df1, df2, on='姓名')

# 纵向合并
combined = pd.concat([df1, df2])</code></pre>
<h2>五、批量处理</h2>
<h3>5.1 批量读取</h3>
<pre><code class="language-python">import os
import pandas as pd

files = [f for f in os.listdir('.') if f.endswith('.xlsx')]
all_data = []

for file in files:
    df = pd.read_excel(file)
    all_data.append(df)

combined = pd.concat(all_data)</code></pre>
<h3>5.2 批量写入</h3>
<pre><code class="language-python">departments = ['销售部', '技术部', '财务部']

for dept in departments:
    df = get_department_data(dept)
    df.to_excel(f'{dept}_报表.xlsx', index=False)</code></pre>
<h2>六、格式化与样式</h2>
<pre><code class="language-python">from openpyxl.styles import Font, PatternFill

# 设置字体
ws['A1'].font = Font(bold=True, color='FF0000')

# 设置背景色
ws['A1'].fill = PatternFill(start_color='FFFF00', fill_type='solid')

# 设置列宽
ws.column_dimensions['A'].width = 20</code></pre>
<h2>七、总结</h2>
<p>Python 处理 Excel 可以大幅提升办公效率。建议多实践。</p>
<hr />
<p><em>关注我们获取更多 Python 实战教程！</em></p>
<p><a href="https://www.dixunblog.cn/1453.html">Python 自动化办公实战：Excel 处理 &#8211; 基础篇</a>最先出现在<a href="https://www.dixunblog.cn">帝讯博客</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dixunblog.cn/1453.html/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
