├── 2016-09-16-python-openpyxl-tutorial.md ├── 2016-09-16-python-openpyxl-usage.md ├── README.md ├── SUMMARY.md └── charts ├── 2016-09-16-python-openpyxl-area.md ├── 2016-09-16-python-openpyxl-bar.md ├── 2016-09-16-python-openpyxl-bubble.md └── 2016-09-16-python-openpyxl-introduction.md /2016-09-16-python-openpyxl-tutorial.md: -------------------------------------------------------------------------------- 1 | # 在内存中编辑workbook 2 | 3 | [英文源地址](https://openpyxl.readthedocs.io/en/default/tutorial.html) 4 | 5 | ---------- 6 | 7 | ## 创建workbook 8 | 9 | 使用openpyxl并不需要在系统上新建一个文件,只需引用Workbook类即可。 10 | 11 | 12 | ``` python 13 | >>> from openpyxl import Workbook 14 | >>> wb = Workbook() 15 | ``` 16 | 17 | 一个Excel文档在创建是至少会有一个标签(worksheet)。因此可以通过使用`openpyxl.workbook.Workbook.active()`方法激活该标签。 18 | 19 | ```python 20 | >>> ws = wb.active 21 | ``` 22 | 23 | > ### Note 24 | > 该功能使用了`_active_sheet_index`方法,默认值为0。如果你没有改变其值,那么通过此方法始终会获取第一个worksheet。 25 | > 同时,你也可以通过说使用方法`openpyxl.workbook.Workbook.create_sheet()`创建一个新的worksheet。 26 | 27 | ```python 28 | >>> ws1 = wb.create_sheet() # 在末尾创建 (默认) 29 | # or 30 | >>> ws2 = wb.create_sheet(0) # 在第一个位置插入 31 | ``` 32 | 33 | 创建Sheets的时候会按照数字队列自动命名(Sheet,Sheet1,Sheet2, ...)。你可以通过`title`属性随时改变其名称。 34 | 35 | ```python 36 | ws.title = "New Title" 37 | ``` 38 | 39 | 页面的背景色默认为白色.你可以为`ws.sheet_properties.tabColor`属性赋值`RRGGBB颜色编码`进行更改: 40 | 41 | ```python 42 | ws.sheet_properties.tabColor = "1072BA" 43 | ``` 44 | 45 | 一旦你给一个worksheet命名之后,你可以通过将`worksheet的名称`作为`workbook的key`或者使用`openpyxl.workbook.Workbook.get_sheet_by_name()`方法得到它。 46 | 47 | ```python 48 | >>> ws3 = wb["New Title"] 49 | >>> ws4 = wb.get_sheet_by_name("New Title") 50 | >>> ws is ws3 is ws4 51 | True 52 | ``` 53 | 54 | 你可以通过方法`openpyxl.workbook.Workbook.get_sheet_names()`获取说为sheet名称: 55 | 56 | ```python 57 | >>> print(wb.get_sheet_names()) 58 | ['Sheet2', 'New Title', 'Sheet1'] 59 | ``` 60 | 61 | 你可以遍历所有worksheets 62 | ```python 63 | >>> for sheet in wb: 64 | ... print(sheet.title) 65 | ``` 66 | 67 | ## 使用数据 68 | ### 访问单个单元格 69 | 70 | 现在,我们知道怎样方位一个worksheet了,现在我们来学习如何改变单元格的内容。 71 | 72 | 我们可以直接通过worksheet的键值(key)方位单元格。 73 | 74 | ```python 75 | >>> c = ws['A4'] 76 | ``` 77 | 78 | 这样就可以直接获取A4单元格了。如果该单元格不存在,则会被创建。 也可以直接为单元格赋值。 79 | 80 | ```python 81 | >>> ws['A4'] = 4 82 | ``` 83 | 84 | 也可以使用方法`openpyxl.workbook.Workbook.cell()`进行操作 85 | 86 | ``` 87 | >>> c = ws.cell('A4') 88 | ``` 89 | 90 | 同样,你也可以通过使用单元格的行和列标记访问: 91 | 92 | ``` 93 | >>> d = ws.cell(row = 4, column = 2) 94 | ``` 95 | 96 | > ### Note 97 | >> 在worksheet在被创建时存在于内存中的。此时worksheet不包含任何单元格。 单元格将在第一次被访问时被创建。 此功能将不会创建不会访问的对象,从而达到可以节约内存的目的。 98 | 99 |
100 | 101 | > ### Warning 102 | >> 正式因为此功能,当遍历所有单元格而非直接访问时,将会在内存中创建它们,即使你不为他们赋值。 103 | >> 104 | >> 比如说 105 | >> 106 | >> ```python 107 | >> >>> for i in range(1,101): 108 | >> ... for j in range(1,101): 109 | >> ... ws.cell(row = i, column = j) 110 | >> ``` 111 | >> 将会在内存中创建100x100个空白单元格。 112 | >> 113 | >> 然后,openpyxl提供了一种方法清空这些不需要的单元格,这个我们将在之后介绍。 114 | 115 | 116 | 117 | ### 访问多个单元格 118 | 119 | 单元格区块(Ranges of cells)可以通过切片方式访问。 120 | 121 | ```ptyhon 122 | >>> cell_range = ws['A1':'C2'] 123 | ``` 124 | 125 | 同样,你也可以使用方法`openpyxl.worksheet.Workbook.iter_rows()`: 126 | 127 | ```python 128 | >>> tuple(ws.iter_rows('A1:C2')) 129 | ((, , ), 130 | (, , )) 131 | 132 | >>> for row in ws.iter_rows('A1:C2'): 133 | ... for cell in row: 134 | ... print cell 135 | 136 | 137 | 138 | 139 | 140 | 141 | ``` 142 | 143 | 如果你需要循环访问一个文件中的所有行或者列,你可以使用`openpyxl.worksheet.Workbook.rows()`属性。 144 | 145 | ```python 146 | >>> ws = wb.active 147 | >>> ws['C9'] = 'hello world' 148 | >>> ws.rows 149 | ((, , ), 150 | (, , ), 151 | (, , ), 152 | (, , ), 153 | (, , ), 154 | (, , ), 155 | (, , ), 156 | (, , ), 157 | (, , )) 158 | ``` 159 | 160 | 或者`openpyxl.workbook.Workbook.columns()`属性: 161 | 162 | ```python 163 | >>> ws.columns 164 | ((, 165 | , 166 | , 167 | , 168 | , 169 | , 170 | ... 171 | , 172 | , 173 | ), 174 | (, 175 | , 176 | , 177 | , 178 | , 179 | , 180 | , 181 | , 182 | )) 183 | ``` 184 | 185 | ### 数据储存 186 | 187 | Once we have a openpyxl.cell.Cell, we can assign it a value: 188 | 当我们获取了一个单元格`openpyxl.cell.Cell`之后,就可以给他赋值了: 189 | 190 | ```python 191 | >>> c.value = 'hello, world' 192 | >>> print(c.value) 193 | 'hello, world' 194 | 195 | >>> d.value = 3.14 196 | >>> print(d.value) 197 | 3.14 198 | ``` 199 | 200 | 你也可以使用type和format接口: 201 | 202 | ```python 203 | >>> wb = Workbook(guess_types=True) 204 | >>> c.value = '12%' 205 | >>> print(c.value) 206 | 0.12 207 | 208 | >>> import datetime 209 | >>> d.value = datetime.datetime.now() 210 | >>> print d.value 211 | datetime.datetime(2010, 9, 10, 22, 25, 18) 212 | 213 | >>> c.value = '31.50' 214 | >>> print(c.value) 215 | 31.5 216 | ``` 217 | 218 | ## 保存文件 219 | 使用`openpyxl.workbook.Workbook.save`方法是保存`openpyxl.workbook.Workbook`对象的最简单、最安全的方式。 220 | 221 | ```python 222 | >>> wb = Workbook() 223 | >>> wb.save('balances.xlsx') 224 | ``` 225 | 226 | > ### Warning 227 | > 该操作会覆盖已存在的文件,并且没有warning提示。 228 | 229 |
230 | 231 | 232 | > ### Note 233 | >>Extension is not forced to be xlsx or xlsm, although you might have some trouble opening it directly with another application if you don’t use an official extension. 234 | >> 235 | >>As OOXML files are basically ZIP files, you can also end the filename with .zip and open it with your favourite ZIP archive manager. 236 | 237 | 238 | 你可以使用属性`as_template=True`将文件保存为一个模板(template)。 239 | 240 | ```python 241 | >>> wb = load_workbook('document.xlsx') 242 | >>> wb.save('document_template.xltx', as_template=True) 243 | ``` 244 | 245 | 或者使用属性`as_template=False`(默认)将一个模板/文件保存为一个文件。 246 | 247 | ```python 248 | >>> wb = load_workbook('document_template.xltx') 249 | >>> wb.save('document.xlsx', as_template=False) 250 | >>> wb = load_workbook('document.xlsx') 251 | >>> wb.save('new_document.xlsx', as_template=False) 252 | ``` 253 | 254 | > ### Warning 255 | >> 在保存/打开文件时你需要监视(monitor)文档模板内的数据属性和文档扩展,否则在保存后的文档可能无法打开。 256 | >> You should monitor the data attributes and document extensions for saving documents in the document templates and vice versa, otherwise the result table engine can not open the document. 257 | 258 | 259 |
260 | 261 | 262 | > ### Note 263 | >> 以下操作会失败: 264 | >>```python 265 | >> >>> wb = load_workbook('document.xlsx') 266 | >> >>> # Need to save with the extension *.xlsx 267 | >> >>> wb.save('new_document.xlsm') 268 | >> >>> # MS Excel can't open the document 269 | >> >>> 270 | >> >>> # or 271 | >> >>> 272 | >> >>> # Need specify attribute keep_vba=True 273 | >> >>> wb = load_workbook('document.xlsm') 274 | >> >>> wb.save('new_document.xlsm') 275 | >> >>> # MS Excel can't open the document 276 | >> >>> 277 | >> >>> # or 278 | >> >>> 279 | >> >>> wb = load_workbook('document.xltm', keep_vba=True) 280 | >> >>> # If us need template document, then we need specify extension as *.xltm. 281 | >> >>> # If us need document, then we need specify attribute as_template=False. 282 | >> >>> wb.save('new_document.xlsm', as_template=True) 283 | >> >>> # MS Excel can't open the document 284 | >> 285 | >>``` 286 | 287 | 288 | ## 加载一个文件 289 | 与保存文件一样,你可以通过导入`openpyxl.load_workbook()`开打一个已存在的workbook: 290 | 291 | ```python 292 | >>> from openpyxl import load_workbook 293 | >>> wb2 = load_workbook('test.xlsx') 294 | >>> print wb2.get_sheet_names() 295 | ['Sheet2', 'New Title', 'Sheet1'] 296 | ``` 297 | 298 | openpyxl 概览到此结束,你现在可开始 简单用法章节([Simple usage section](./usage.md) )了。 299 | -------------------------------------------------------------------------------- /2016-09-16-python-openpyxl-usage.md: -------------------------------------------------------------------------------- 1 | # 简单用例 2 | [ Simple Usage ]( https://openpyxl.readthedocs.io/en/default/usage.html ) 3 | 4 | ---- 5 | 6 | ## 编辑工作簿 7 | 8 | ```python 9 | >>> from openpyxl import Workbook 10 | >>> from openpyxl.compat import range # import后的range 将覆盖标准库中的range函数 11 | >>> from openpyxl.cell import get_column_letter 12 | >>> 13 | >>> wb = Workbook() # 创建一个workbook 14 | >>> 15 | >>> dest_filename = 'empty_book.xlsx' # 保存使用的位置和文件名 16 | >>> 17 | >>> ws1 = wb.active # 获取工作表 (创建新工作簿时始终会有一个工作表) 18 | >>> ws1.title = "range names" # 为工作命名 19 | >>> 20 | >>> for row in range(1, 40): # 向工作表写入40行 21 | ... ws1.append(range(600)) # 向工作表写入600列 22 | >>> 23 | >>> ws2 = wb.create_sheet(title="Pi") # 创建第二个工作表,并命名为Pi 24 | >>> 25 | >>> ws2['F5'] = 3.14 # 给单元格'F5'赋值 26 | >>> 27 | >>> ws3 = wb.create_sheet(title="Data") # 创建第三个工作表,并命名为Data 28 | >>> for row in range(10, 20): 29 | ... for col in range(27, 54): 30 | ... _ = ws3.cell(column=col, row=row, value="%s" % get_column_letter(col)) # 将单元格的列名作为单元格的值 31 | >>> print(ws3['AA10'].value) 32 | AA 33 | >>> 34 | >>> ws4=wb.create_sheet('NEW_DATA') 35 | >>> for row in range(1,10): 36 | for col in range(1,10): 37 | # 如果没有使用赋值语句 "_ = .... ",结果如下 38 | ws4.cell(column=col,row=row, value="%s" % get_column_letter(col)) 39 | 40 | 41 | 42 | 43 | 44 | 45 | ... 46 | >>> 47 | >>> wb.save(filename = dest_filename) # 保存文件 48 | ``` 49 | 50 | 51 | ### 读取xltx模板并另存为xlsx文件 52 | 53 | ```python 54 | >>> from openpyxl import load_workbook 55 | >>> 56 | >>> 57 | >>> wb = load_workbook('sample_book.xltx') # 读取xltx文件 58 | >>> ws = wb.active # 获取工作表 59 | >>> ws['D2'] = 42 # 为单元格赋值 60 | >>> 61 | >>> wb.save('sample_book.xlsx') # 保存文件 62 | >>> 63 | >>> # 你也可以覆盖当前的文件模板 64 | >>> # wb.save('sample_book.xltx') 65 | ``` 66 | 67 | 68 | ## 读取xltm模板并另存为xlsm文件 69 | 70 | ```python 71 | >>> from openpyxl import load_workbook 72 | >>> 73 | >>> 74 | >>> wb = load_workbook('sample_book.xltm', keep_vba=True) # keep_vba=True 保留vba代码 75 | >>> ws = wb.active 76 | >>> ws['D2'] = 42 77 | >>> 78 | >>> wb.save('sample_book.xlsm') 79 | >>> 80 | >>> # 你也可以覆盖当前的文件模板 81 | >>> # wb.save('sample_book.xltm') 82 | ``` 83 | 84 | ## 读取一个已存在的工作簿 85 | 86 | ```python 87 | >>> from openpyxl import load_workbook 88 | >>> wb = load_workbook(filename = 'empty_book.xlsx') # 加载工作簿 89 | >>> sheet_ranges = wb['range names'] # 使用名为"range names"的工作表 90 | >>> print(sheet_ranges['D18'].value) # 打印单元格的值 91 | 3 92 | ``` 93 | 94 | 95 | >###**Note** 96 | >> 97 | >> 使用load_workbook时,有几个标记(flag)可以用。 98 | >> 99 | >> 1. guess_types : 当读取单元格信息时,是否启用推测单元格格式,默认为`不启用 False`。 100 | >> 101 | >> 2. data_only :控制读取单元格时是否保留公式(formulae),默认为`保留 False`。或者直接读取单元格所存的最终值。 102 | >> 103 | >> 3. keep_vba : 是否保留Visual Basic元素功能,默认为`不保留 False`。即使VB元素被保留,也不能被编辑。 104 | 105 |
106 | 107 | >###**Warning** 108 | >> 109 | >> openpyxl 目前不能完全读取Excel文件中的所有项目, 因此在打开和保存过程中使用相同的文件名,图片和图表会被丢失。 110 | >> 111 | 112 | 113 |
114 | 115 | 116 | ## 使用数字格式 117 | 118 | ```python 119 | >>> import datetime 120 | >>> from openpyxl import Workbook 121 | >>> wb = Workbook(guess_types=True) #创建一个工作簿,并且使用单元格格式 122 | >>> ws = wb.active 123 | >>> # set date using a Python datetime 124 | >>> # 使用日期格式 125 | >>> ws['A1'] = datetime.datetime(2010, 7, 21) 126 | >>> 127 | >>> ws['A1'].number_format 128 | 'yyyy-mm-dd h:mm:ss' 129 | >>> 130 | >>> # set percentage using a string followed by the percent sign 131 | >>> # 使用百分数格式,需要在数字后面添加一个百分号。 132 | >>> ws['B1'] = '3.14%' 133 | >>> 134 | >>> ws['B1'].value 135 | 0.031400000000000004 136 | >>> 137 | >>> ws['B1'].number_format # 显示单元格B1的格式 138 | '0%' 139 | ``` 140 | 141 | ## 使用公式 142 | 143 | ```python 144 | >>> from openpyxl import Workbook 145 | >>> wb = Workbook() 146 | >>> ws = wb.active 147 | >>> # add a simple formula 148 | >>> # 是单元格的值为一个公式。实际上就是一个可以解析的字符串 149 | >>> ws["A1"] = "=SUM(1, 1)" 150 | >>> wb.save("formula.xlsx") 151 | ``` 152 | 153 | >**Warning** 154 | >> 155 | >> 函数名称必须使用英文字母;函数的参数必须使用`逗号( , )`分隔,不能使用其他符号,比如半角冒号。 156 | 157 |
158 | 159 | openpyxl 不会执行公式,但可能会检查公式名称是否正确: 160 | 161 | ```python 162 | >>> from openpyxl.utils import FORMULAE 163 | >>> "HEX2DEC" in FORMULAE # 查看公式名称是否在FORMULAE中 164 | True 165 | >>> print type(openpyxl.utils.FORMULAE) 166 | 167 | ``` 168 | 169 | If you’re trying to use a formula that isn’t known this could be because you’re using a formula that was not included in the initial specification. Such formulae must be prefixed with `xlfn.` to work. 170 | 171 | 如果你尝试使用一个并不常用的公式,可能这个公式没有包含在初始规范中。这种公式必须使用前缀`xlfn.`才能正常工作。 172 | 173 | 174 | ## 合并/拆分单元格 175 | 176 | ```python 177 | >>> from openpyxl.workbook import Workbook 178 | >>> 179 | >>> wb = Workbook() 180 | >>> ws = wb.active 181 | >>> 182 | >>> ws.merge_cells('A1:B1') # 合并单元格 183 | >>> ws.unmerge_cells('A1:B1') # 拆分单元格 184 | >>> 185 | >>> # 或者使用单元格坐标进行操作 186 | >>> ws.merge_cells(start_row=2,start_column=1,end_row=2,end_column=4) 187 | >>> ws.unmerge_cells(start_row=2,start_column=1,end_row=2,end_column=4) 188 | ``` 189 | 190 | 191 | ## 插入图片 192 | 193 | ```python 194 | >>> from openpyxl import Workbook 195 | >>> from openpyxl.drawing.image import Image 196 | >>> 197 | >>> wb = Workbook() 198 | >>> ws = wb.active 199 | >>> ws['A1'] = 'You should see three logos below' 200 | >>> # create an image 201 | >>> img = Image('logo.png') 202 | >>> # add to worksheet and anchor next to cells 203 | >>> # 在单元格旁边插入图片 204 | >>> ws.add_image(img, 'A1') 205 | >>> wb.save('logo.xlsx') 206 | ``` 207 | 208 | 209 | ## Fold columns (outline) 210 | ## 折叠列 (outline) 211 | 212 | ```python 213 | >>> import openpyxl 214 | >>> wb = openpyxl.Workbook(True) 215 | >>> ws = wb.create_sheet() 216 | >>> ws.column_dimensions.group('A','D', hidden=True) 217 | >>> wb.save('group.xlsx') 218 | ``` 219 | 220 | ---- 221 | 222 | ^[ <-Previous ]( tutorial.md ) | [ Next-> ]( ./charts/introduction.md )| -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # python的Excel库 openpyxl 文档汉化 2 | python 的execl 库 openpyxl 文档翻译 3 | 4 | 5 | ## 简介 6 | 7 | [简介](2016-09-16-python-openpyxl-tutorial.md) 8 | 9 | + [创建workbook](2016-09-16-python-openpyxl-tutorial.md#创建workbook) 10 | + [数据操作](2016-09-16-python-openpyxl-tutorial.md#数据操作) 11 | + [访问单个单元格](2016-09-16-python-openpyxl-tutorial.md#访问单个单元格) 12 | + [访问多个单元格](2016-09-16-python-openpyxl-tutorial.md#访问多个单元格) 13 | + [数据储存](2016-09-16-python-openpyxl-tutorial.md#数据储存) 14 | + [保存文件](2016-09-16-python-openpyxl-tutorial.md#保存文件) 15 | + [加载一个文件](2016-09-16-python-openpyxl-tutorial.md#加载一个文件) 16 | 17 | ## 简单用例 18 | 19 | [简单用例](2016-09-16-python-openpyxl-usage.md) 20 | 21 | + [编辑工作簿](2016-09-16-python-openpyxl-usage.md#编辑工作簿) 22 | + [读取xltx模板并另存为xlsx文件](2016-09-16-python-openpyxl-usage.md#读取xltx模板并另存为xlsx文件) 23 | + [读取xltm模板并另存为xlsm文件](2016-09-16-python-openpyxl-usage.md#读取xltm模板并另存为xlsm文件) 24 | + [读取一个已存在的工作簿](2016-09-16-python-openpyxl-usage.md#读取一个已存在的工作簿) 25 | + [使用数字格式](2016-09-16-python-openpyxl-usage.md#使用数字格式) 26 | + [使用公式](2016-09-16-python-openpyxl-usage.md#使用公式) 27 | + [合并/拆分单元格](2016-09-16-python-openpyxl-usage.md#合并/拆分单元格) 28 | + [插入图片](2016-09-16-python-openpyxl-usage.md#插入图片) 29 | + [折叠列 (outline)](2016-09-16-python-openpyxl-usage.md#折叠列-outline) 30 | 31 | ## 图表 32 | 33 | [图标介绍](charts/2016-09-16-python-openpyxl-introduction.md) 34 | 35 | + [面积图](charts/2016-09-16-python-openpyxl-area.md) 36 | + [条状图](charts/2016-09-16-python-openpyxl-bar.md) 37 | + [点状图](charts/2016-09-16-python-openpyxl-bubble.md) -------------------------------------------------------------------------------- /SUMMARY.md: -------------------------------------------------------------------------------- 1 | # python的Excel库 openpyxl 文档汉化 2 | 3 | ## 简介 4 | 5 | [简介](2016-09-16-python-openpyxl-tutorial.md) 6 | 7 | + [创建workbook](2016-09-16-python-openpyxl-tutorial.md#创建workbook) 8 | + [数据操作](2016-09-16-python-openpyxl-tutorial.md#数据操作) 9 | + [访问单个单元格](2016-09-16-python-openpyxl-tutorial.md#访问单个单元格) 10 | + [访问多个单元格](2016-09-16-python-openpyxl-tutorial.md#访问多个单元格) 11 | + [数据储存](2016-09-16-python-openpyxl-tutorial.md#数据储存) 12 | + [保存文件](2016-09-16-python-openpyxl-tutorial.md#保存文件) 13 | + [加载一个文件](2016-09-16-python-openpyxl-tutorial.md#加载一个文件) 14 | 15 | ## 简单用例 16 | 17 | [简单用例](2016-09-16-python-openpyxl-usage.md) 18 | 19 | + [编辑工作簿](2016-09-16-python-openpyxl-usage.md#编辑工作簿) 20 | + [读取xltx模板并另存为xlsx文件](2016-09-16-python-openpyxl-usage.md#读取xltx模板并另存为xlsx文件) 21 | + [读取xltm模板并另存为xlsm文件](2016-09-16-python-openpyxl-usage.md#读取xltm模板并另存为xlsm文件) 22 | + [读取一个已存在的工作簿](2016-09-16-python-openpyxl-usage.md#读取一个已存在的工作簿) 23 | + [使用数字格式](2016-09-16-python-openpyxl-usage.md#使用数字格式) 24 | + [使用公式](2016-09-16-python-openpyxl-usage.md#使用公式) 25 | + [合并/拆分单元格](2016-09-16-python-openpyxl-usage.md#合并/拆分单元格) 26 | + [插入图片](2016-09-16-python-openpyxl-usage.md#插入图片) 27 | + [折叠列 (outline)](2016-09-16-python-openpyxl-usage.md#折叠列-outline) 28 | 29 | ## 图表 30 | 31 | [图标介绍](charts/2016-09-16-python-openpyxl-introduction.md) 32 | 33 | + [面积图](charts/2016-09-16-python-openpyxl-area.md) 34 | + [条状图](charts/2016-09-16-python-openpyxl-bar.md) 35 | + [点状图](charts/2016-09-16-python-openpyxl-bubble.md) -------------------------------------------------------------------------------- /charts/2016-09-16-python-openpyxl-area.md: -------------------------------------------------------------------------------- 1 | #面积图 2 | 3 | [ Area Charts ](http://docs.uyinn.com/openpyxl.readthedocs.io/en/default/charts/area.html) 4 | 5 | ---- 6 | 7 | ##2D面积图 8 | 9 | Area charts are similar to line charts with the addition that the area underneath the plotted line is filled. Different variants are available by setting the grouping to “standard”, “stacked” or “percentStacked”; “standard” is the default. 10 | 11 | 面积图与线性图类似;描绘的线下面进行填充,用面来表示。通过设置`grouping`不同的值(`chart.grouping="standard"`)绘制不同的面积图,包括"standard","stacked","percentStacked",默认为 standard。 12 | 13 | ```python 14 | from openpyxl import Workbook 15 | from openpyxl.chart import ( 16 | AreaChart, 17 | Reference, 18 | Series, 19 | ) 20 | 21 | wb = Workbook() 22 | ws = wb.active 23 | 24 | rows = [ 25 | ['Number', 'Batch 1', 'Batch 2'], 26 | [2, 40, 30], 27 | [3, 40, 25], 28 | [4, 50, 30], 29 | [5, 30, 10], 30 | [6, 25, 5], 31 | [7, 50, 10], 32 | ] 33 | 34 | for row in rows: # 为worksheet写入多行数据 35 | ws.append(row) # 将list作为一行数据写入worksheet中 36 | 37 | chart = AreaChart() # 创建 2D图表 对象 38 | chart.title = "Area Chart" # 设置图表标题 39 | chart.style = 13 # 设置图标风格 40 | # chart.grouping="standard" # 设置图表 grouping 类型 41 | chart.x_axis.title = 'Test' # 设置x轴名称 42 | chart.y_axis.title = 'Percentage' # 设置y轴名称 43 | 44 | cats = Reference(ws, min_col=1, min_row=1, max_row=7) # 关联分类数据的值 45 | data = Reference(ws, min_col=2, min_row=1, max_col=3, max_row=7) # 关联构图数据的值 46 | chart.add_data(data, titles_from_data=True) # 为图表添加数据 47 | chart.set_categories(cats) # 为图表添加分类 48 | 49 | ws.add_chart(chart, "A10") # 设置图表位置 50 | 51 | wb.save("area.xlsx") 52 | ``` 53 | 54 | !["Sample area charts"](http://docs.uyinn.com/openpyxl.readthedocs.io/en/default/_images/area.png) 55 | 56 | 57 | ##3D面积图 58 | 59 | 创建3D面积图 60 | 61 | ```python 62 | from openpyxl import Workbook 63 | from openpyxl.chart import ( 64 | AreaChart3D, 65 | Reference, 66 | Series, 67 | ) 68 | 69 | wb = Workbook() 70 | ws = wb.active 71 | 72 | rows = [ 73 | ['Number', 'Batch 1', 'Batch 2'], 74 | [2, 30, 40], 75 | [3, 25, 40], 76 | [4 ,30, 50], 77 | [5 ,10, 30], 78 | [6, 5, 25], 79 | [7 ,10, 50], 80 | ] 81 | 82 | for row in rows: 83 | ws.append(row) # 绘制单元格数据 84 | 85 | chart = AreaChart3D() # 创建 3D面积图 对象 86 | chart.title = "Area Chart" 87 | chart.style = 13 88 | chart.x_axis.title = 'Test' 89 | chart.y_axis.title = 'Percentage' 90 | chart.legend = None 91 | 92 | cats = Reference(ws, min_col=1, min_row=1, max_row=7) # 关联分类数据的值 93 | data = Reference(ws, min_col=2, min_row=1, max_col=3, max_row=7) #关联构图数据的值 94 | chart.add_data(data, titles_from_data=True) 95 | chart.set_categories(cats) 96 | 97 | ws.add_chart(chart, "A10") 98 | 99 | wb.save("area3D.xlsx") 100 | ``` 101 | 102 | 103 | 这样就创建了一个简单的 3D面积图。 面积图的 Z轴 可以用来作为数据说明。 104 | 105 | !["Sample 3D area chart with a series axis"](http://docs.uyinn.com/openpyxl.readthedocs.io/en/default/_images/area3D.png) 106 | 107 | ---- 108 | 109 | ^[ <-Previous ]( ./introduction.md ) | [ Next-> ]( ./bar.md )| 110 | -------------------------------------------------------------------------------- /charts/2016-09-16-python-openpyxl-bar.md: -------------------------------------------------------------------------------- 1 | # Bar and Column Charts 2 | [Bar and Column Charts](https://openpyxl.readthedocs.io/en/default/charts/bar.html) 3 | 4 | ---- 5 | 6 | 7 | 柱状图会绘制水平或者垂直的数据列。 8 | 9 | ## Vertical, Horizontal and Stacked Bar Charts 10 | ## 垂直、水平、堆栈柱状图 11 | 12 | >###**Note** 13 | > 14 | > 以下设置将影响不同类型的柱状图。 15 | > 通过分别设置type的值为`bar或col`从而使用垂直`type=col`或水平`type=bar`柱状图。 16 | > 17 | > 当使用堆栈柱状图时,overlap的值需要设置为100。`overlap=100` 18 | > 19 | > 如果是水平柱状图,x和y轴将颠倒。 20 | 21 |
22 | 23 | 24 | !["Sample bar charts"](http://docs.uyinn.com/openpyxl.readthedocs.io/en/default/_images/bar.png) 25 | 26 | 27 | ```python 28 | from openpyxl import Workbook 29 | from openpyxl.chart import BarChart, Series, Reference 30 | 31 | wb = Workbook(write_only=True) 32 | ws = wb.create_sheet() 33 | 34 | rows = [ 35 | ('Number', 'Batch 1', 'Batch 2'), 36 | (2, 10, 30), 37 | (3, 40, 60), 38 | (4, 50, 70), 39 | (5, 20, 10), 40 | (6, 10, 40), 41 | (7, 50, 30), 42 | ] 43 | 44 | 45 | for row in rows: # 添加单元格绘图数据 46 | ws.append(row) 47 | 48 | 49 | chart1 = BarChart() # 创建 2D柱状图 对象 50 | chart1.type = "col" # 柱状图type=col,垂直图。 51 | chart1.style = 10 # style显示不同的风格 52 | chart1.title = "Bar Chart" # 图形名称 53 | chart1.y_axis.title = 'Test number' # y轴名称 54 | chart1.x_axis.title = 'Sample length (mm)' # x名称 55 | 56 | data = Reference(ws, min_col=2, min_row=1, max_row=7, max_col=3) # 关联图形数据 57 | cats = Reference(ws, min_col=1, min_row=2, max_row=7) # 关联数据分类 58 | chart1.add_data(data, titles_from_data=True) # 数据标题从titles_from_data=True获取,即数据单元格组的第一行 59 | chart1.set_categories(cats) 60 | chart1.shape = 4 61 | ws.add_chart(chart1, "A10") # 生成柱状图及定位 62 | 63 | from copy import deepcopy 64 | 65 | chart2 = deepcopy(chart1) # deepcopy ,之后直接修改部分属性 66 | chart2.style = 11 67 | chart2.type = "bar" # 水平柱状图 68 | chart2.title = "Horizontal Bar Chart" 69 | 70 | ws.add_chart(chart2, "G10") # 生成柱状图及定位 71 | 72 | 73 | chart3 = deepcopy(chart1) # deepcopy ,之后直接修改部分属性 74 | chart3.type = "col" # 水平柱状图 75 | chart3.style = 12 76 | chart3.grouping = "stacked" # 类型为堆栈 77 | chart3.overlap = 100 # 必须设置内容 78 | chart3.title = 'Stacked Chart' 79 | 80 | ws.add_chart(chart3, "A27") # 生成柱状图及定位 81 | 82 | 83 | chart4 = deepcopy(chart1) 84 | chart4.type = "bar" # 水平柱状图 85 | chart4.style = 13 86 | chart4.grouping = "percentStacked" # 类型 百分比堆栈 87 | chart4.overlap = 100 88 | chart4.title = 'Percent Stacked Chart' 89 | 90 | ws.add_chart(chart4, "G27") 91 | 92 | wb.save("bar.xlsx") 93 | ``` 94 | 95 | 这样便生成了4种不同类型的柱状图。 96 | 97 | 98 | ## 3D Bar Charts 99 | ## 3D柱状图 100 | 101 | 102 | 可以生成3D柱状图。 103 | 104 | ```python 105 | from openpyxl import Workbook 106 | from openpyxl.chart import ( 107 | Reference, 108 | Series, 109 | BarChart3D, 110 | ) 111 | 112 | wb = Workbook() 113 | ws = wb.active 114 | 115 | rows = [ 116 | (None, 2013, 2014), 117 | ("Apples", 5, 4), 118 | ("Oranges", 6, 2), 119 | ("Pears", 8, 3) 120 | ] 121 | 122 | for row in rows: 123 | ws.append(row) 124 | 125 | data = Reference(ws, min_col=2, min_row=1, max_col=3, max_row=4) 126 | titles = Reference(ws, min_col=1, min_row=2, max_row=4) 127 | chart = BarChart3D() # 创建 3D柱状图 对象 128 | chart.title = "3D Bar Chart" 129 | chart.add_data(data=data, titles_from_data=True) 130 | chart.set_categories(titles) 131 | 132 | ws.add_chart(chart, "E5") 133 | wb.save("bar3d.xlsx") 134 | ``` 135 | 136 | 如此便生成了一个简单的 3D柱状图 137 | 138 | 139 | !["Sample 3D bar chart"](http://docs.uyinn.com/openpyxl.readthedocs.io/en/default/_images/bar3D.png) 140 | 141 | ---- 142 | 143 | ^[ <-Previous ]( ./bar.md ) | [ Next-> ]( ./bubble.md )| -------------------------------------------------------------------------------- /charts/2016-09-16-python-openpyxl-bubble.md: -------------------------------------------------------------------------------- 1 | # 气泡图 2 | [ Bubble Charts ](https://openpyxl.readthedocs.io/en/default/charts/bubble.html) 3 | 4 | ---- 5 | 6 | 气泡图与散列图类似,气泡图还使用气泡的大小作为第三维来描述数据的值。图标可以包含多个数据组。 7 | 8 | ```python 9 | 10 | """ 11 | 12 | 简单气泡图 13 | """ 14 | 15 | from openpyxl import Workbook 16 | from openpyxl.chart import Series, Reference, BubbleChart 17 | 18 | wb = Workbook() 19 | ws = wb.active 20 | 21 | rows = [ 22 | ("Number of Products", "Sales in USD", "Market share"), 23 | (14, 12200, 15), 24 | (20, 60000, 33), 25 | (18, 24400, 10), 26 | (22, 32000, 42), 27 | (), 28 | (12, 8200, 18), 29 | (15, 50000, 30), 30 | (19, 22400, 15), 31 | (25, 25000, 50), 32 | ] 33 | 34 | for row in rows: 35 | ws.append(row) 36 | 37 | chart = BubbleChart() # 创建一个 气泡图 38 | chart.style = 18 # use a preset style 39 | 40 | # add the first series of data 41 | # 添加第一个数据组 42 | xvalues = Reference(ws, min_col=1, min_row=2, max_row=5) 43 | yvalues = Reference(ws, min_col=2, min_row=2, max_row=5) 44 | size = Reference(ws, min_col=3, min_row=2, max_row=5) 45 | series = Series(values=yvalues, xvalues=xvalues, zvalues=size, title="2013") 46 | chart.series.append(series) ### 图形数据(chart.series)是一个list。将多个数据组追加进去。 47 | 48 | # add the second 49 | # 添加第二个数据组 50 | xvalues = Reference(ws, min_col=1, min_row=7, max_row=10) 51 | yvalues = Reference(ws, min_col=2, min_row=7, max_row=10) 52 | size = Reference(ws, min_col=3, min_row=7, max_row=10) 53 | series = Series(values=yvalues, xvalues=xvalues, zvalues=size, title="2014") 54 | chart.series.append(series) ### 图形数据(chart.series)是一个list。追加第二个 55 | 56 | # place the chart starting in cell E1 57 | # 气泡图生成位置 58 | ws.add_chart(chart, "E1") 59 | wb.save("bubble.xlsx") 60 | ``` 61 | 62 | > **注意** 63 | > 64 | > 本例中,并没有使用第三列数据`Market share`。 65 | 66 | 以上操作将生成一个两组数据的气泡图,效果如如下: 67 | 68 | !["Sample bubble chart"]( https://openpyxl.readthedocs.io/en/default/_images/bubble.png ) 69 | 70 | 71 | ---- 72 | 73 | ^[ <-Previous ]( ./bar.md ) | [ Next-> ]( ./line.md )| -------------------------------------------------------------------------------- /charts/2016-09-16-python-openpyxl-introduction.md: -------------------------------------------------------------------------------- 1 | # 图表 2 | [Charts](https://openpyxl.readthedocs.io/en/default/charts/introduction.html) 3 | 4 | ---- 5 | 6 | 7 | >**###Warning** 8 | >> 9 | >>Openpyxl currently supports chart creation within a worksheet only. Charts in existing workbooks will be lost. 10 | >> 11 | >>openpyxl 目前只支持在内存中创建图表。操作一个已存在的工作簿时,图表会丢失。 12 | >> 13 | 14 | ## Chart types 15 | ## 图表类型 16 | 17 | The following charts are available: 18 | 以下图表类型可用: 19 | 20 | * [ 面积图 ](./area.md) 21 | * [ 2D Area Charts ](./area.md#2D面积图) 22 | * [ 3D Area Charts ](./area.md#3D面积图) 23 | * [Bar and Column Charts](./bar.md) 24 | * [Vertical, Horizontal and Stacked Bar Charts](bar.md#vertical-horizontal-and-stacked-bar-charts) 25 | * [3D Bar Charts](./bar.md#3d-bar-charts) 26 | * [Bubble Charts]( ./bubble.md ) 27 | * [Line Charts]( ./line.md ) 28 | * [Line Charts]( ./line.md ) 29 | * [3D Line Charts]( ./line.md ) 30 | * Scatter Charts 31 | * Pie Charts 32 | * Pie Charts 33 | * Projected Pie Charts 34 | * 3D Pie Charts 35 | * Doughnut Charts 36 | * Radar Charts 37 | * Stock Charts 38 | * Surface charts 39 | 40 | 41 | ## Creating a chart 42 | Charts are composed of at least one series of one or more data points. Series themselves are comprised of references to cell ranges. 43 | 44 | ```python 45 | >>> from openpyxl import Workbook 46 | >>> wb = Workbook() 47 | >>> ws = wb.active 48 | >>> for i in range(10): 49 | ... ws.append([i]) 50 | >>> 51 | >>> from openpyxl.chart import BarChart, Reference, Series 52 | >>> values = Reference(ws, min_col=1, min_row=1, max_col=1, max_row=10) 53 | >>> chart = BarChart() 54 | >>> chart.add_data(values) 55 | >>> ws.add_chart(chart) 56 | >>> wb.save("SampleChart.xlsx") 57 | ``` 58 | 59 | ## Working with axes 60 | * Axis Limits and Scale 61 | * Minima and Maxima 62 | * Logarithmic Scaling 63 | * Axis Orientation 64 | * Adding a second axis 65 | 66 | 67 | ## Change the chart layout 68 | * Changing the layout of plot area and legend 69 | * Chart layout 70 | * Size and position 71 | * Mode 72 | * Target 73 | * Legend layout 74 | 75 | 76 | ## Styling charts 77 | Adding Patterns 78 | 79 | 80 | ## Advanced charts 81 | Charts can be combined to create new charts: 82 | * Gauge Charts 83 | 84 | 85 | ## Using chartsheets 86 | Charts can be added to special worksheets called chartsheets: 87 | * Chartsheets --------------------------------------------------------------------------------