简体   繁体   English

如何使用 python 格式化 excel 文件?

[英]how to format excel file using python?

I have a script that scrapes data from list of websites using beautifulSoup package and save in an excel file using pandas and xlsxwriter packages. I have a script that scrapes data from list of websites using beautifulSoup package and save in an excel file using pandas and xlsxwriter packages.

What i want is to be able to format the excel file as i need like the width of the columns我想要的是能够像列的宽度一样格式化 excel 文件

but when i run the script it crash and display the below error.但是当我运行脚本时它崩溃并显示以下错误。

AttributeError: 'NoneType' object has no attribute 'write'

code:代码:

import pandas as pd

import requests
from bs4 import BeautifulSoup
import xlsxwriter

def scrap_website():
    url_list = ["https://www.bayt.com/en/international/jobs/executive-chef-jobs/",
    "https://www.bayt.com/en/international/jobs/head-chef-jobs/",
    "https://www.bayt.com/en/international/jobs/executive-sous-chef-jobs/"]
    
    joineddd = []
    for url in url_list:
        soup = BeautifulSoup(requests.get(url).content,"lxml")
        links = []
        for a in soup.select("h2.m0.t-regular a"):
            if a['href'] not in links:
                links.append("https://www.bayt.com"+ a['href'])
        
        for link in links:
            s = BeautifulSoup(requests.get(link).content, "lxml") 
            ### update Start ###
            alldd = dict()
            alldd['link'] = link
            dd_div = [i for i in s.select("div[class='card-content is-spaced'] div") 
                    if ('<dd>' in str(i) ) and ( "<dt>" in str(i))]

            for div in dd_div:
                k = div.select_one('dt').get_text(';', True)
                v = div.select_one('dd').get_text(';', True)
                alldd[k] = v
            ### update End  ###    
            joineddd.append(alldd)


# result
        df = pd.DataFrame(joineddd)
        df_to_excel = df.to_excel(r"F:\\AIenv\web_scrapping\\jobDesc.xlsx", index = False, header=True)
        workbook = xlsxwriter.Workbook(df_to_excel)
        worksheet = workbook.add_worksheet()
        worksheet.set_column(0, 0,50)
        workbook.close()

    

where is the error and how to fix it?错误在哪里以及如何解决?

  1. to_excel function returns nothing. to_excel function 什么也不返回。 It's why you got the error message.这就是您收到错误消息的原因。
# save excel file
excel_file_name = r"jobDesc.xlsx"
df.to_excel(excel_file_name, index = False, header=True)

# open excel file for change col width or something
workbook = xlsxwriter.Workbook(excel_file_name)
  1. Basically, you can't change existing file with xlsxwriter .基本上,您不能使用xlsxwriter更改现有文件。 There is a way to do so, but it is not recommended.有一种方法可以这样做,但不建议这样做。 I recommend openpyxl package instead of this.我推荐openpyxl package 而不是这个。 FYI, xlsxwriter: is there a way to open an existing worksheet in my workbook?仅供参考, xlsxwriter:有没有办法在我的工作簿中打开现有工作表?

To access and format the Excel workbook or worksheet created by to_excel() you need to create an ExcelWriter object first.要访问和格式化由to_excel()创建的 Excel 工作簿或工作表,您需要首先创建一个 ExcelWriter object。 Something like this:像这样的东西:

import pandas as pd


# Create a Pandas dataframe from some data.
df = pd.DataFrame({'Data': [10, 20, 30, 20, 15, 30, 45]})

# Create a Pandas Excel writer using XlsxWriter as the engine.
writer = pd.ExcelWriter('pandas_simple.xlsx', engine='xlsxwriter')

# Convert the dataframe to an XlsxWriter Excel object.
df.to_excel(writer, sheet_name='Sheet1', index=False, header=True)

# Get the xlsxwriter objects from the dataframe writer object.
workbook  = writer.book
worksheet = writer.sheets['Sheet1']

# Set the column width.
worksheet.set_column(0, 0, 50)

# Close the Pandas Excel writer and output the Excel file.
writer.save()

Output: Output:

在此处输入图像描述

See Working with Python Pandas and XlsxWriter for more details.有关更多详细信息,请参阅使用 Python Pandas 和 XlsxWriter

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 python 将 excel 文件中的日期转换为文本格式 - How to convert a date to text format in excel file using python 如何使用Python从Excel文件中提取单元格格式(粗体,斜体等)? - How to extract cell format (bold, italic, …) from an Excel file using Python? 如何使用openpyxl根据python中的特定条件格式化excel文件的行? - How to format row of excel file based on specific condition in python using openpyxl? 使用 python 下载 excel 文件时如何将字符串转换为日期格式 - How to convert a string to date format when downloading an excel file using python 如何在保留格式的同时使用 python 将 excel 文件翻译成另一种语言 - How to translate a excel file to another language using python while retaining the format 使用 Python 将 Excel 文件转换为符合特定格式的 Json - Convert Excel file to Json respecting a specific format using Python 如何使用python中的xlsx包格式化excel中的特定单元格 - how to format specific cells in excel using xlsx package in python 如何使用python将Excel工作表复制到具有相同格式的另一个工作簿 - How to copy excel sheet to another workbook with same format using python 如何读取文本文件格式并将其写入python中的以下格式中提到的excel文件中 - How to read the text file format and write the same in to excel file as mentioned in the below format in python 使用openpyxl将日期格式写入excel文件时如何更改日期格式 - How to change the date format while writing it to excel file using openpyxl
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM