Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit eeb0f20

Browse files
add code
1 parent b8ceee6 commit eeb0f20

File tree

2 files changed

+162
-0
lines changed

2 files changed

+162
-0
lines changed

‎chaoxi/README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ Python技术 公众号文章代码库
99

1010
## 实例代码
1111

12+
[方便!Python 操作 Excel 神器 xlsxwriter 初识!](https://github.com/JustDoPython/python-examples/tree/master/chaoxi/xlsxwriter) 方便!Python 操作 Excel 神器 xlsxwriter 初识!
13+
1214
[神器 Pandas 绘图大全(上)!](https://github.com/JustDoPython/python-examples/tree/master/chaoxi/Pandas2) 神器 Pandas 绘图大全(中)!
1315

1416
[神器 Pandas 绘图大全(上)!](https://github.com/JustDoPython/python-examples/tree/master/chaoxi/Pandas1) 神器 Pandas 绘图大全(上)!
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
import xlsxwriter
2+
from datetime import datetime
3+
def simple_use():
4+
import xlsxwriter
5+
6+
workbook = xlsxwriter.Workbook('demo.xlsx') # 建立文件
7+
8+
worksheet = workbook.add_worksheet() # 建立sheet, 可以使用work.add_worksheet('employee')来指定sheet名,如果命名中文名会报UnicodeDecodeErro的错误
9+
10+
worksheet.write('A1', 'Hello world') # 向A1写入文字
11+
12+
workbook.close()
13+
14+
def simple_example():
15+
16+
# 创建一个新的Excel文件并添加一个工作表
17+
workbook = xlsxwriter.Workbook('example_demo.xlsx')
18+
worksheet = workbook.add_worksheet()
19+
20+
# 确定第一栏,使文字更清楚
21+
worksheet.set_column('A:A', 20)
22+
23+
# 添加粗体格式以突出显示单元格
24+
bold = workbook.add_format({'bold': True})
25+
26+
# 简单的写一些文字
27+
worksheet.write('A1', 'Hello')
28+
29+
# 另起一行写入文字并加粗
30+
worksheet.write('A2', 'Python 技术', bold)
31+
32+
# 用行/列表示法写一些数字
33+
worksheet.write(2, 0, 123)
34+
worksheet.write(3, 0, 13.432)
35+
36+
# 插入一张图片.
37+
worksheet.insert_image('B5', 'logo.jpeg')
38+
39+
workbook.close()
40+
41+
def sum_data():
42+
workbook = xlsxwriter.Workbook('demo.xlsx') # 建立文件
43+
worksheet = workbook.add_worksheet()
44+
add_data = (
45+
['A1', 1087],
46+
['A2', 1056],
47+
['A3', 300],
48+
['A4', 590],
49+
)
50+
# 按标号写入是从0开始的,按绝对位置'A1'写入是从1开始的
51+
row = 0
52+
col = 0
53+
54+
# 遍历数据并逐行写出它
55+
for item, cost in (add_data):
56+
worksheet.write(row, col, item)
57+
worksheet.write(row, col + 1, cost)
58+
row += 1
59+
60+
# 用公式写出总数
61+
worksheet.write(row, 0, 'Total')
62+
worksheet.write(row, 1, '=SUM(B1:B4)') # 调用excel的公式表达式
63+
64+
workbook.close()
65+
66+
def self_define_format():
67+
# 建文件及sheet.
68+
workbook = xlsxwriter.Workbook('demo2.xlsx')
69+
worksheet = workbook.add_worksheet()
70+
71+
# Add a bold format to use to highlight cells. 设置粗体,默认是False
72+
bold = workbook.add_format({'bold': True})
73+
74+
# 定义数字格式
75+
money = workbook.add_format({'num_format': '$#,##0'})
76+
77+
# Write some data headers. 带自定义粗体blod格式写表头
78+
worksheet.write('A1', 'Item', bold)
79+
worksheet.write('B1', 'Cost', bold)
80+
81+
# Some data we want to write to the worksheet.
82+
add_data = (
83+
['A1', 1087],
84+
['A2', 1056],
85+
['A3', 300],
86+
['A4', 590],
87+
)
88+
89+
# Start from the first cell below the headers.
90+
row = 1
91+
col = 0
92+
93+
# Iterate over the data and write it out row by row.
94+
for item, cost in (add_data):
95+
worksheet.write(row, col, item) # 带默认格式写入
96+
worksheet.write(row, col + 1, cost, money) # 带自定义money格式写入
97+
row += 1
98+
99+
# Write a total using a formula.
100+
worksheet.write(row, 0, 'Total', bold)
101+
worksheet.write(row, 1, '=SUM(B2:B5)', money)
102+
103+
workbook.close()
104+
105+
def write_date():
106+
107+
from datetime import datetime
108+
workbook = xlsxwriter.Workbook('demo3.xlsx')
109+
worksheet = workbook.add_worksheet()
110+
111+
# 添加粗体格式以突出显示单元格.
112+
bold = workbook.add_format({'bold': 1})
113+
114+
# 为带钱的单元格添加数字格式.
115+
money_format = workbook.add_format({'num_format': '$#,##0'})
116+
117+
# 添加Excel日期格式.
118+
date_format = workbook.add_format({'num_format': 'mmmm d yyyy'})
119+
120+
# 调整列的宽度
121+
worksheet.set_column(1, 1, 15)
122+
123+
# 写入数据表头
124+
worksheet.write('A1', 'Item', bold)
125+
worksheet.write('B1', 'Date', bold)
126+
worksheet.write('C1', 'Cost', bold)
127+
128+
# 将数据写入工作表
129+
add_data = (
130+
['A1', '2021年01月13日', 1875],
131+
['A2', '2021年07月14日', 345],
132+
['A3', '2022年01月01日', 564],
133+
['A4', '2021年01月26日', 10987],
134+
)
135+
136+
# 从标题下面的第一个单元格开始.
137+
row = 1
138+
col = 0
139+
140+
for item, date_str, cost in (add_data):
141+
# 将日期字符串转换为datetime对象
142+
date = datetime.strptime(date_str, "%Y-%m-%d")
143+
144+
worksheet.write_string(row, col, item)
145+
worksheet.write_datetime(row, col + 1, date, date_format)
146+
worksheet.write_number(row, col + 2, cost, money_format)
147+
row += 1
148+
149+
# 用公式写出总数
150+
worksheet.write(row, 0, 'Total', bold)
151+
worksheet.write(row, 2, '=SUM(C2:C5)', money_format)
152+
153+
workbook.close()
154+
155+
if __name__ == '__main__':
156+
# simple_example()
157+
#simple_use()
158+
#sum_data()
159+
#self_define_format()
160+
write_date()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /