openpyxl

openpyxl - How to preserve xlsx custom properties

不想你离开。 提交于 2020-08-08 05:49:28
问题 How do I preserve custom properties from xlsx template which I am modifying with openpyxl ? When I save() workbook using openpyxl these custom properties vanish! Custom properties can be found here:- On Mac -> Go to File Menu in Excel -> Properties ... -> Custom tab -> Properties section 回答1: I am posting a pure python solution to reading and writing Workbook.CustomDocumentProperties just because I am currently also feeling the pain of not having this in openpyxl, and I needed a quick

Python 读写excel Part1

醉酒当歌 提交于 2020-08-07 15:02:04
背景 有很多朋友对Python处理excel很感兴趣,虽然我不是这方面的专家,如果用C# openxl 是分分钟搞定的事情,再次点用Npoi也行,Python则完全陌生。 目标 既然以学习为目标去完成这件事,那么就不必太复杂(复杂我也做不来),就假设我们是在做销售系统吧。如果不专业请轻喷。 一份Excel的产品单,一份客户单,一份订单,字段简单点。 从头开始生成 一,工具 既然使用Python,当然要介绍一下工具库: 主要有openpyxl、xlwt、xlrd、Pandas(这是真的可以)、xlsxwriter 先来分析对比一波 1、Openpyxl 优势:快速、高效地读写Excel,可控制大部分Excel元素,并且写入时可以贴入公式 劣势:对初学者非常不友好,属性超级多,并且按xml格式控制;再来样式只能定义一次,修改样式就得先复制一份再重新构造,工作量几何级上升 2、xlsxwriter 专门用于写数据的方法 优势:快速,可控制大部分样式,公式只能以字符串处理,写数据以xy定位或单元格命名为主 劣势:没有毒没有毒没有毒,所以,千万别用同一份excel文档进行处理(我就吃了这个亏,好好几十万条记录被清洗一空),暂时不知道怎么追加到原文档后添加新sheet 3、xlrd、xlwt 可读写的excel库 优势:基本上用来读excel数据,非常精准、数据类型清晰 劣势:写明显很慢

Read dataframe split by nan rows and reshape them into multiple dataframes in Python

我的未来我决定 提交于 2020-08-07 07:45:18
问题 I have a example excel file data1.xlsx from here, which has a Sheet1 as follows: Now I want to read it with openpyxl or pandas , then convert them into new df1 and df2 , I will finally save them as price and quantity sheet: price sheet: and quantity sheet Code I have used: df = pd.read_excel('./data1.xlsx', sheet_name = 'Sheet1') df_list = np.split(df, df[df.isnull().all(1)].index) for df in df_list: print(df, '\n') Out: bj Unnamed: 1 Unnamed: 2 Unnamed: 3 Unnamed: 4 0 year 2018.0 2019.0 2020

Read dataframe split by nan rows and reshape them into multiple dataframes in Python

为君一笑 提交于 2020-08-07 07:44:06
问题 I have a example excel file data1.xlsx from here, which has a Sheet1 as follows: Now I want to read it with openpyxl or pandas , then convert them into new df1 and df2 , I will finally save them as price and quantity sheet: price sheet: and quantity sheet Code I have used: df = pd.read_excel('./data1.xlsx', sheet_name = 'Sheet1') df_list = np.split(df, df[df.isnull().all(1)].index) for df in df_list: print(df, '\n') Out: bj Unnamed: 1 Unnamed: 2 Unnamed: 3 Unnamed: 4 0 year 2018.0 2019.0 2020

python处理word和excel文件

假装没事ソ 提交于 2020-08-04 16:30:39
python处理word和excel表格数据 学习过程中遇到批量将excel表格数据填入到word的问题,对于excel的函数使用不是很熟悉,python中有处理word和excel的库,所以学习使用python实现对数据的批量处理。首先是需要了解处理word和excel的具体操作。 python对编辑word表格 需要安装第三方库python-docx python-docx API网址: https://python-docx.readthedocs.io/en/latest/#api-documentation 对表格的基本操作 # -*- coding: utf-8 -*- from docx import Document from docx.enum.text import WD_ALIGN_PARAGRAPH from docx.oxml.ns import qn from docx.shared import Pt path=r'C:\Users\hxy\Desktop\test.docx'#文件路径 doc=Document(path)#读入文件 #设置表格字体 doc.styles['Normal'].font.name = u'仿宋' doc.styles['Normal']._element.rPr.rFonts.set(qn('w:eastAsia'),

How to speed up import of large xlsx files?

最后都变了- 提交于 2020-08-02 19:32:09
问题 I want to process a large 200MB Excel (xlsx) file with 15 sheets and 1 million rows with 5 columns each) and create a pandas dataframe from the data. The import of the Excel file is extremely slow (up to 10minutes). Unfortunately, the Excel import file format is mandatory (I know that csv is faster...). How can I speed up the process of importing a large Excel file into a pandas dataframe? Would be great to get the time down to around 1-2 minutes, if possible, which would be much more

How to speed up import of large xlsx files?

和自甴很熟 提交于 2020-08-02 19:30:49
问题 I want to process a large 200MB Excel (xlsx) file with 15 sheets and 1 million rows with 5 columns each) and create a pandas dataframe from the data. The import of the Excel file is extremely slow (up to 10minutes). Unfortunately, the Excel import file format is mandatory (I know that csv is faster...). How can I speed up the process of importing a large Excel file into a pandas dataframe? Would be great to get the time down to around 1-2 minutes, if possible, which would be much more

使用Python中openpyxl库来操作Excel

徘徊边缘 提交于 2020-07-28 09:57:55
文章目录 执行 pip install openpyxl 安装该库 新建Excel,写入数据 下面的示例代码 修改Excel中的数据 修改单元格内容 删除行、删除列 文字 颜色、字体、大小 背景色 插入图片 Excel COM接口 执行 pip install openpyxl 安装该库 import os os . system ( "pip3 install openpyxl" ) 新建Excel,写入数据 xlrd 只能读取Excel内容,如果你要 创建 一个新的Excel并 写入 数据,可以使用 openpyxl 库。 openpyxl 库既可以读文件、也可以写文件、也可以修改文件。 但是,openpyxl 库不支持老版本 Office2003 的 xls 格式的Excel文档,如果要读写xls格式的文档,可以使用 Excel 进行相应的格式转化。 下面的代码,演示了 openpyxl 的 一些基本用法。 import openpyxl #创建一个Excel workbook 对象 book = openpyxl . Workbook ( ) #创建时,会自动产生一个sheet,通过active获取 sh = book . active #修改当前 sheet 标题为 工资表 sh . title = '工资表' #保存文件 book . save ( '信息.xlsx'

Python效率革命——处理Excel表格

牧云@^-^@ 提交于 2020-07-28 08:45:04
Excel是Windows环境下运行的强大的电子表格应用。一个Excel电子表格称为一个工作簿,一个工作簿保存在拓展名为.xlsx的文件中。每个工作簿可以包含多个表(工作表)用户当前查看的表(或者关闭前最后查看的表)称为活动表。每个表都有行(数字表示)和列(字母表示) 第三方模块openpyxl模块让Python程序可以读取和修改Excel,下图列举了其它表格处理的功能矩阵,可以看出openpyxl是全面的,所以我挑选了这个第三方模块作为学习对象。 openpyxl处理Excel表格 安装模块 读取Excel文档 从工作簿中取得工作表 从表中取得单元格 列字母和数字之间的切换 写入Excel文档 创建并保存Excel文档 创建和删除工作表 将值写入单元格 设置单元格字体风格与类型 公式 调整行和列 设置行高和列宽 合并和拆分单元格 冻结窗格 图表 安装模块 pip install openpyxl #轻轻松松 读取Excel文档 用openpyxl模块打开Excel文档 import openpyxl wb = openpyxl . load_workbook ( 'example.xlsx' ) openpyxl.load_workbook()函数接受文件名,返回一个workbook数据类型的值,这个workbook对象代表这个Excel文件(即工作簿) 从工作簿中取得工作表

手把手教你使用Python抓取QQ音乐数据(第一弹)

戏子无情 提交于 2020-07-28 03:39:42
【一、项目目标】 获取 QQ 音乐指定歌手单曲排行指定页数的歌曲的歌名、专辑名、播放链接。 由浅入深,层层递进,非常适合刚入门的同学练手。 【二、需要的库】 主要涉及的库有:requests、json、openpyxl 【三、项目实现】 1.了解 QQ 音乐网站的 robots 协议 只禁止播放列表,可以操作。 2.进入 QQ 音乐主页 https://y.qq.com/ 3.输入任意歌手,比如邓紫棋 4.打开审查元素(快捷键 Ctrl+Shift+I) 5.分析网页源代码 Elements,发现无歌曲信息,无法使用 BeautifulSoup,如下图所示,结果为空。 6.点击 Network,看数据在不在 XHR(无刷新更新页 面),我的经验是先看 Size 最大的,然后分析 Name, 查看 Preview,果然在里面! 7.点击 Headers,拿到相关参数。如下图,仔细观察 url 与 Query String Parameters 参数的关系,发现 url 中的 w 代表歌手名,p 代表页数。 8.通过 json 代码实现,首先小试牛刀,爬取第一页 的数据,url 直接复制过来。成功! 9.引入 params 参数,实现指定歌手、指定页数的查询。 注意代码url为上一步url中“?”之前的部分, params两边的参数都需要加 ’’,requests.get 添加