openpyxl

用 Python 读写 Excel 表格,就是这么的简单粗暴且乏味

拈花ヽ惹草 提交于 2020-07-26 13:45:54
过去,在很多金融、市场、行政的招聘中,面试官都会问一句:“你精通 EXCEL 吗?” 但今天,他们可能更喜欢问:“你会 Python 吗?” 越来越多的企业开始用 Python 处理数据,特别是金融、证券、商业、互联网等领域。 在顶级公司的高端职位中,Python 更是成为了标配: Python 究竟有什么法力能让大家如此青睐? 举个例子:在过去,如果老板想要获取 A 股所有股票近 2 年的数据,你可能需要 登录-查询-下载-记录到excel 循环 500 多次,即使你是一个没有感情的复制机器人,也需要一两天的时间。 但如果你掌握了 Python,只需要写个脚本,**泡杯咖啡的功夫 **数据就全部下载好了。 再加上 Python 强大的绘图功能,你可以一次性完成 数据收集 — 整理 — 分析 — 绘图 的过程,直接把分析结果用图表呈现出来。 今天本篇文章,我们就总结了一下利用 python 操作 Excel 文件的第三方库和方法。 内容出自课程—— 《OpenPyXL 处理 Excel 基础入门》 ,欢迎大家来实验边敲代码边学习~ 首先,我们来学习一下,如何 用 Python 创建和保存 Excel 文档。 对于经常与数据打交道的人来说,Excel 是经常使用的工具;对于与数据打交道的程序员来说,OpenPyXL 库是一个利器。Python 官方提供了这样一个库,让我们可以直接通过

pip install pymongo SSLError

假如想象 提交于 2020-07-24 21:15:17
pip3 install pymongo 出现SSL error Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:877)'),)': /packages/95/8c/83563c60489954e5b80f9e2596b93a68e1ac4e4a730deb1aae632066d704/openpyxl-3.0.3.tar.gz 找了几个国内的源 阿里云 http://mirrors.aliyun.com/pypi/simple/ 中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/ 豆瓣(douban) http://pypi.douban.com/simple/ 清华大学 https://pypi.tuna.tsinghua.edu.cn/simple/ 中国科学技术大学 http://pypi.mirrors.ustc.edu.cn/simple/ pip install openpyxl -

openpyxl: Append data to first empty column cell

我们两清 提交于 2020-07-06 03:53:22
问题 Background: I have an excel workbook containing metadata which spread across various worksheets. I need to take the relevant columns of data from the various worksheets and combine them into a single worksheet. With the following code I have been able to create a new worksheet and add data to it. # Open workbook and assign worksheet try: wb = openpyxl.load_workbook(metadata) shtEditionLNM = wb.worksheets[0] # Edition date & latest NM shtChartsTitles = wb.worksheets[1] # Charts & Titles

openpyxl: Append data to first empty column cell

别等时光非礼了梦想. 提交于 2020-07-06 03:52:28
问题 Background: I have an excel workbook containing metadata which spread across various worksheets. I need to take the relevant columns of data from the various worksheets and combine them into a single worksheet. With the following code I have been able to create a new worksheet and add data to it. # Open workbook and assign worksheet try: wb = openpyxl.load_workbook(metadata) shtEditionLNM = wb.worksheets[0] # Edition date & latest NM shtChartsTitles = wb.worksheets[1] # Charts & Titles

Horizontal text alignment in openpyxl

≯℡__Kan透↙ 提交于 2020-07-04 22:16:38
问题 I'm tryign to change the text alignment to the center of 2 merged sells, I've found some answers that didn't work for my case currentCell = ws.cell('A1') currentCell.style.alignment.horizontal = 'center' #TypeError: cannot set horizontal attribute #or currentCell.style.alignment.vertical = Alignment.HORIZONTAL_CENTER #AttributeError: type object 'Alignment' has no attribute 'HORIZONTAL_CENTER' both didn't work, is there any other ways to do it? 回答1: yes, there is a way to do this with

To process a folder with xlsx files with openpyxl module

别等时光非礼了梦想. 提交于 2020-06-29 05:13:09
问题 I have folder ('rogaikopyta') with 1 000 files in xlsx format. I need to extract the data form each of these files (B2 and D2 cells) and print them in ordered consequence in one xlsx file. The original code is: import openpyxl import os import pathlib from openpyxl import load_workbook, Workbook path = 'C:/Users/User/Documents/Visual Studio 2017/DjangoWebProject1/DjangoWebProject1/app/rogaikopyta' for file in os.listdir(path): wb=load_workbook(os.path.join(path,file), read_only=True) ws=wb

Applying row height to all rows including and after row 7

左心房为你撑大大i 提交于 2020-05-31 03:41:52
问题 I cannot figure out how to apply a row height on an existing worksheet unless I do it one row at a time. This works for a single row: ws4.row_dimensions[14].height = 25 But I want to set the row height for row 7 and any subsequent rows. This approach does nothing yet does not throw an error: for rows in ws4.iter_rows(min_row=7, max_row=None): ws4.row_dimensions.height = 25 wb4.save('C:\\folder\\DataplusRows.xlsx') Any idea how to do this? I can't glean the answer from the openpyxl

Applying row height to all rows including and after row 7

旧街凉风 提交于 2020-05-31 03:41:10
问题 I cannot figure out how to apply a row height on an existing worksheet unless I do it one row at a time. This works for a single row: ws4.row_dimensions[14].height = 25 But I want to set the row height for row 7 and any subsequent rows. This approach does nothing yet does not throw an error: for rows in ws4.iter_rows(min_row=7, max_row=None): ws4.row_dimensions.height = 25 wb4.save('C:\\folder\\DataplusRows.xlsx') Any idea how to do this? I can't glean the answer from the openpyxl

Openpyxl not found in Pycharm

给你一囗甜甜゛ 提交于 2020-05-29 09:53:30
问题 I am new to programming Python using Pycharm. I would like to access openpyxl (which I installed) but get the following error message: Traceback (most recent call last): File "<input>", line 1, in <module> File "D:\PyCharm\PyCharm Community Edition 2017.3.3\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 20, in do_import module = self._system_import(name, *args, **kwargs) ModuleNotFoundError: No module named 'openpyxl' This is probably a very simple problem but I just don't know how

Openpyxl not found in Pycharm

旧时模样 提交于 2020-05-29 09:53:26
问题 I am new to programming Python using Pycharm. I would like to access openpyxl (which I installed) but get the following error message: Traceback (most recent call last): File "<input>", line 1, in <module> File "D:\PyCharm\PyCharm Community Edition 2017.3.3\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 20, in do_import module = self._system_import(name, *args, **kwargs) ModuleNotFoundError: No module named 'openpyxl' This is probably a very simple problem but I just don't know how