首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >openpyxl错误删除记录: /xl/worksheets/sheet1.xml中的公式

openpyxl错误删除记录: /xl/worksheets/sheet1.xml中的公式
EN

Stack Overflow用户
提问于 2018-12-08 15:09:36
回答 2查看 7.2K关注 0票数 3

首先,我是编程方面的新手。

我正试图用MySQL将python3数据库导出为excel格式。我用openpyxl做的。现在我在excel中有一个有趣的错误。如果我运行代码,但SQL查询很小(大约1000行),那么在打开excel时没有错误,但是当我试图打开excel时,如果它更大(>30k行),则会出现一个错误:

在文件'C:\Users\id022504\PycharmProjects\GetMySQLdata\Interface _mau.xlsx‘Records: /xl/worksheets/sheet1.xml部分中检测到error135840_01.xmlErrors

当我使用Open打开excel文件时,非常有趣,它指出问题在颜色上:

在这里输入图像描述

在这里输入图像描述

贝娄是密码:

代码语言:javascript
运行
复制
import mysql.connector
import openpyxl
from openpyxl import Workbook
from openpyxl.styles import Border, Side, Font, Alignment
import datetime
import os
from openpyxl.worksheet import Worksheet

mydb = mysql.connector.connect(
  host="10.10.10.10",
  user="user",
  passwd="password"
)

#Funcao para por as colunas com auto size
def auto_column_resize(worksheet):
    for col in worksheet.columns:
         max_length = 0
         column = col[0].column # Get the column name
         for cell in col:
             try: # Necessary to avoid error on empty cells
                 if len(str(cell.value)) > max_length:
                     max_length = len(cell.value)
             except:
                 pass
         adjusted_width = (max_length + 2) * 1.2
         if adjusted_width <= 95:
            worksheet.column_dimensions[column].width = adjusted_width
         else:
             worksheet.column_dimensions[column].width =95
    return worksheet

# definir path
path='C:/Users/'

# definir path para arquivo
path_arquivo='C:/Users/Arquivo/'

#definir tamanho do arquivo
arquivo_file_size = 26





# Abrir ficheiro actual e guardar nos arquivos
current_wb = openpyxl.load_workbook(path+"Interface planning.xlsx")
current_ws = current_wb["Ports allocation"]


if int(datetime.datetime.now().isocalendar()[1]) >9:
    current_wb.save("{0}/Interface planning_{1}{2}.xlsx".format(path_arquivo, str(int(datetime.datetime.now().isocalendar()[0])),str(int(datetime.datetime.now().isocalendar()[1]))))
else:
    #introduce 0 in the week so it can calculate later which file is the oldest
    current_wb.save("{0}/Interface planning_{1}0{2}.xlsx".format(path_arquivo, str(int(datetime.datetime.now().isocalendar()[0])),str(int(datetime.datetime.now().isocalendar()[1]))))

#Abrir SQL e comando
mycursor = mydb.cursor()
mycursor.execute("SELECT hostname, hardware, port_label, ifHighSpeed, ifAdminStatus, ifOperStatus, ifAlias FROM `observium`.`ports` JOIN `observium`.`devices` ON `observium`.`devices`.device_id = `observium`.`ports`.device_id WHERE (port_label LIKE 'xe-%' or port_label LIKE 'et-%' or port_label LIKE 'ge-%' or port_label LIKE '%Ethernet%') and port_label NOT RLIKE '[.][1-9]' ORDER BY hostname, port_label;")


#Meter em tupel os dados recebidos pela base de dados
myresult = mycursor.fetchall()
header = mycursor.column_names

#Criar workbook
new_wb = Workbook()

#Criar worksheet
new_ws = new_wb.active
new_ws.title = "Ports allocation"

############################################## Meter dados SQL em excel ##########################################

#Add header information and formating
new_ws.append(header)

new_ws["H1"].value = "Person assigned"

for format_row in new_ws:
    for i in range(8):
        format_row[i].font = Font(bold=True)

#Add content from SQL to excel
for row in myresult:
    new_ws.append(row)

new_ws.auto_filter.ref = "A:H"

#Verificar o estado da interface e colocar a pessoa responsavel se a interface esta administrativamente e operacionalmente em baixo
for current_ws_row in current_ws:
    if current_ws_row[7].value is not None:
        for new_ws_row in new_ws:
            if (new_ws_row[4].value != "up" or new_ws_row[5].value != "up") and current_ws_row[0].value == new_ws_row[0].value and current_ws_row[2].value == new_ws_row[2].value :
                new_ws_row[7].value= current_ws_row[7].value
                new_ws_row[6].value = current_ws_row[6].value


for format_row in new_ws:
    for i in range(8):
        format_row[i].border = Border(right=Side(style='thin'),)

#Meter o worksheet bonitinho
new_ws = auto_column_resize(new_ws)
new_ws.sheet_view.zoomScale = 85
c=new_ws['D2']
new_ws.freeze_panes = c

wrap_alignment = Alignment(wrap_text=True)
for row in new_ws.iter_rows():
    for cell in row:
        cell.alignment = Alignment(shrink_to_fit=True)

#Salvar workbook
new_wb.save(path+"Interface planning.xlsx")

#remover ficheiro no arquivo

count_files=0
#infinite time
file_to_delete = '299952'

for directory in os.walk(path_arquivo):
    for file in directory[2]:
        count_files = count_files+1
        if  str(file)[-11:-5] < file_to_delete:
            file_to_delete = str(file)[-11:-5]


if count_files > arquivo_file_size:
    os.remove(path_arquivo+'Interface planning_'+file_to_delete+'.xlsx')
EN

Stack Overflow用户

回答已采纳

发布于 2018-12-12 11:26:39

我发现了这个问题,在其中一个单元格中有"=“符号,而excel不知怎么地将它识别为一个公式。为了解决这个问题,我只是把"=“弄干净了。

代码语言:javascript
运行
复制
import re
try:
    new_ws_row[7].value = re.sub("=", "", new_ws_row[7].value)
except:
    pass
票数 7
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53683822

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档