openpyxl

scrapy持久化到Excel表格

亡梦爱人 提交于 2019-12-04 13:34:56
前提条件: 防止乱码产生 ITEM_PIPELINES = { 'xpc.pipelines.ExcelPipeline': 300, } 方法一 1、安装openpyxl conda install openpyxl 2、pipline from openpyxl import Workbook class ExcelPipeline(object): def __init__(self): # 创建excel, 填写表头 self.wb = Workbook() self.ws = self.wb.active # 设置表头 self.ws.append(['ID', '标题', 'URL']) def process_item(self, item, spider): # 把数据的每一项整理出来 line = [item['pid'], item['title'], item['src']] # 将数据以行的形式添加到xlsx中 self.ws.append(line) # 保存xlsx文件中 self.wb.save('work.xlsx') return item 3、setting ITEM_PIPELINES = { 'xpc.pipelines.ExcelPipeline': 300, } 方法二 scrapy crawl work -o work.csv

Python openpyxl Excel绘制柱形图

微笑、不失礼 提交于 2019-12-04 12:05:20
这是一份 openpyxl 的使用指南。 大体内容翻译自官网 https://openpyxl.readthedocs.io/en/stable/charts/bar.html#vertical-horizontal-and-stacked-bar-charts 本文在官网基础上会有一些改动。代码请参考 https://github.com/RustFisher/python-playground 本文链接 https://rustfisher.com/2019/11/14/Python/Python-openpyxl_charts_Bar_and_Column/ 柱形图 数据会被绘制成垂直,水平或者是层叠效果的柱形图。 注意: 接下来的设置会影响到不同类型的图表。 设置类别可以控制柱形图是垂直或是水平。 使用层叠图形时,需要设置overlap成100。 如果柱形是水平的,x和y坐标会对调过来。 2D图表 示例代码如下,根据给定的数据绘制了4张图表。 from openpyxl import Workbook from openpyxl.chart import BarChart, Series, Reference def bar_chart_demo(): wb = Workbook(write_only=True) ws = wb.create_sheet() rows =

Losing merged cells border while editing Excel file with openpyxl

╄→гoц情女王★ 提交于 2019-12-04 11:59:55
问题 I have two sheets in an Excel file and the first one is a cover sheet which I don't need to edit. There are few merged cells in the cover sheet. But when I edit the file using openpyxl not even touch the cover sheet, I lose borders from the merged cells. Is there any fix to this problem? I am using Load_workbook('excel file') to load the excel file and saving it with a different filename. 回答1: Actual solution is to patch the libraries code by including this snippet after including the library

Python openpyxl write list to Excel error

允我心安 提交于 2019-12-04 10:00:05
I am new to Python and working on my first project. I am trying to get my code to copy columns of data from one spreadsheet and append it to data that currently exists in a master sheet. I am able to capture the data in each of the sheets and create a new master list which combines both data sets, but I am having trouble writing it to a file. When I test print the combined lists they appear correct, but when I add the code to write the lists to a file it gets hung up. Any assistance you can provide will be extremely helpful! Below is my code. Here's the error I'm getting Traceback (most recent

How do I color-fill in a specific Excel cell using openpyxl?

廉价感情. 提交于 2019-12-04 09:10:58
I'm currently using openpyxl to modify specific excel cells. I am able to modify font styles very easily, simply just: ws['A1'].font = Font(color=colors.White) But I am unable to change the fill of the specific cell. Anyone know any documentation about how to do this? I want to just change the color of one cell, so what other packages are required? I tried exploring some other things like PatternFill , but I haven't been able to accurately get what I'm looking for. All I need is to change the fill color of a single cell. Leb What was the PatternFill code that you tried using? Is it something

Applying Format to Entire Row Openpyxl

不羁的心 提交于 2019-12-04 08:10:41
I have an Excel File that I want to format. The first row (excluding Headers so row2) should be red and italicized . the Openpyxl Documentation states : If you want to apply styles to entire rows and columns then you must apply the style to each cell yourself I personally thinks this stinks... Here is my workaround: import openpyxl from openpyxl.styles import NamedStyle from openpyxl import load_workbook from openpyxl.styles.colors import RED from openpyxl.styles import Font # I normally import a lot of stuff... I'll also take suggestions here. file = 'MY_PATH' wb = load_workbook(filename=file

csv & xlsx files import to pandas data frame: speed issue

大城市里の小女人 提交于 2019-12-04 07:53:05
Reading data (just 20000 numbers) from a xlsx file takes forever: import pandas as pd xlsxfile = pd.ExcelFile("myfile.xlsx") data = xlsxfile.parse('Sheet1', index_col = None, header = None) takes about 9 seconds. If I save the same file in csv format it takes ~25ms: import pandas as pd csvfile = "myfile.csv" data = pd.read_csv(csvfile, index_col = None, header = None) Is this an issue of openpyxl or am I missing something? Are there any alternatives? Matti John xlrd has support for .xlsx files, and this answer suggests that at least the beta version of xlrd with .xlsx support was quicker than

Set openpyxl cell format to currency

筅森魡賤 提交于 2019-12-04 06:54:55
I'm creating an excel sheet using openpyxl. Some cells represent monetary values. How can I change the format of a cell to be of type "currency", i.e. I want a "€" symbol to be displayed after the number. absolutelyNoWarranty Try setting the format code with your desired format code _cell.number_format = '#,##0.00€' 来源: https://stackoverflow.com/questions/34652300/set-openpyxl-cell-format-to-currency

openpyxl - Ability to remove border from charts?

a 夏天 提交于 2019-12-04 05:11:03
问题 Using openpyxl, the charts inserted into my worksheet have a border on them. Is there any way to set the style of the chart (pie/bar) to either via the styles.Style/styles.borders module to have no border, or at least a thin white border so that they would print borderless? The only options I see on the object is .style = <int> which doesn't seem to actually affect the design of the final graphic. 回答1: You can set chat graphical_properties, the code is chart.graphical_properties =

python之openpyxl模块操作

送分小仙女□ 提交于 2019-12-04 02:01:37
import openpyxl #写 # book = openpyxl.Workbook() # sheet = book.active #默认的sheet # #sheet2 = book.get_sheet_by_name('sheet1') # # sheet.append( ['id','username','password','error_count']) # # sheet.append( [1,'wyj','123456',0]) # # sheet.append( [2,'wyj','123456']) # sheet['a1'] = 'id' #指定行列 # sheet['b1'] = 'username'# # sheet.cell(3,1,'1')#指定行和列 # book.save('user.xlsx') book = openpyxl.load_workbook('user.xlsx') sheet = book.active # print(sheet.cell(1,1).value) # print(sheet['a1'].value) # print(list(sheet.rows)) #所有行的数据 # l = [] # print(list(sheet.columns)) #所有列 # for row in sheet.rows: # t